| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
package processing.app; |
| 24 |
|
| 25 |
import processing.app.helpers.FileUtils; |
| 26 |
|
| 27 |
import java.io.File; |
| 28 |
import java.io.IOException; |
| 29 |
import java.nio.file.Files; |
| 30 |
import java.nio.file.Path; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.List; |
| 33 |
import java.util.stream.Collectors; |
| 34 |
import java.util.stream.Stream; |
| 35 |
|
| 36 |
import static processing.app.I18n.tr; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SketchFile { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private File file; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private Sketch sketch; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private boolean primary; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static interface TextStorage { |
| 64 |
|
| 65 |
public String getText(); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public boolean isModified(); |
| 72 |
|
| 73 |
|
| 74 |
public void clearModified(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private TextStorage storage; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public SketchFile(Sketch sketch, File file) { |
| 95 |
this.sketch = sketch; |
| 96 |
this.file = file; |
| 97 |
FileUtils.SplitFile split = FileUtils.splitFilename(file); |
| 98 |
this.primary = split.basename.equals(sketch.getFolder().getName()) |
| 99 |
&& Sketch.SKETCH_EXTENSIONS.contains(split.extension); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public void setStorage(TextStorage text) { |
| 108 |
this.storage = text; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public File getFile() { |
| 113 |
return file; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public boolean isPrimary() { |
| 120 |
return primary; |
| 121 |
} |
| 122 |
|
| 123 |
protected boolean fileExists() { |
| 124 |
return file.exists(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
protected boolean fileReadOnly() { |
| 129 |
return !file.canWrite(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
protected boolean delete(Path tempBuildFolder) throws IOException { |
| 134 |
if (!file.delete()) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
List<Path> tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch")) |
| 139 |
.filter(path -> Files.exists(path)).collect(Collectors.toList()); |
| 140 |
|
| 141 |
for (Path folder : tempBuildFolders) { |
| 142 |
if (!deleteCompiledFilesFrom(folder)) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
sketch.removeFile(this); |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException { |
| 153 |
List<Path> compiledFiles = Files.list(tempBuildFolder) |
| 154 |
.filter(pathname -> pathname.getFileName().toString().startsWith(getFileName())) |
| 155 |
.collect(Collectors.toList()); |
| 156 |
|
| 157 |
for (Path compiledFile : compiledFiles) { |
| 158 |
try { |
| 159 |
Files.delete(compiledFile); |
| 160 |
} catch (IOException e) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void renameTo(String newName) throws IOException { |
| 178 |
File newFile = new File(file.getParentFile(), newName); |
| 179 |
sketch.checkNewFilename(newFile); |
| 180 |
if (!file.exists() || file.renameTo(newFile)) { |
| 181 |
renamedTo(newFile); |
| 182 |
} else { |
| 183 |
String msg = I18n.format(tr("Failed to rename \"{0}\" to \"{1}\""), file.getName(), newName); |
| 184 |
throw new IOException(msg); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
protected void renamedTo(File what) { |
| 193 |
file = what; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public String getFileName() { |
| 200 |
return file.getName(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public String getPrettyName() { |
| 209 |
if (!PreferencesData.getBoolean("editor.show_always_extensions") && isExtension(Sketch.SKETCH_EXTENSIONS)) |
| 210 |
return getBaseName(); |
| 211 |
else |
| 212 |
return getFileName(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public String getBaseName() { |
| 219 |
return FileUtils.splitFilename(file).basename; |
| 220 |
} |
| 221 |
|
| 222 |
public boolean isExtension(String... extensions) { |
| 223 |
return isExtension(Arrays.asList(extensions)); |
| 224 |
} |
| 225 |
|
| 226 |
public boolean isExtension(List<String> extensions) { |
| 227 |
return FileUtils.hasExtension(file, extensions); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
public String getProgram() { |
| 232 |
if (storage != null) |
| 233 |
return storage.getText(); |
| 234 |
|
| 235 |
return null; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
public boolean isModified() { |
| 240 |
if (storage != null) |
| 241 |
return storage.isModified(); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public boolean equals(Object o) { |
| 247 |
return (o instanceof SketchFile) && file.equals(((SketchFile) o).file); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public String load() throws IOException { |
| 256 |
String text = BaseNoGui.loadFile(file); |
| 257 |
|
| 258 |
if (text == null) { |
| 259 |
throw new IOException(); |
| 260 |
} |
| 261 |
|
| 262 |
if (text.indexOf('\uFFFD') != -1) { |
| 263 |
System.err.println( |
| 264 |
I18n.format( |
| 265 |
tr("\"{0}\" contains unrecognized characters. " + |
| 266 |
"If this code was created with an older version of Arduino, " + |
| 267 |
"you may need to use Tools -> Fix Encoding & Reload to update " + |
| 268 |
"the sketch to use UTF-8 encoding. If not, you may need to " + |
| 269 |
"delete the bad characters to get rid of this warning."), |
| 270 |
file.getName() |
| 271 |
) |
| 272 |
); |
| 273 |
System.err.println(); |
| 274 |
} |
| 275 |
return text; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public void save() throws IOException { |
| 284 |
if (storage == null) |
| 285 |
return; |
| 286 |
|
| 287 |
BaseNoGui.saveFile(storage.getText(), file); |
| 288 |
storage.clearModified(); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public void saveAs(File newFile) throws IOException { |
| 296 |
if (storage == null) |
| 297 |
return; |
| 298 |
|
| 299 |
BaseNoGui.saveFile(storage.getText(), newFile); |
| 300 |
renamedTo(newFile); |
| 301 |
storage.clearModified(); |
| 302 |
} |
| 303 |
} |