@Test public void testCopy() throws Exception { Asset asset = new Asset(config); asset.copy(); File cssFile = new File(folder.toString() + File.separatorChar + "css" + File.separatorChar + "bootstrap.min.css"); Assertions.assertTrue(cssFile.exists(), () -> "File " + cssFile.getAbsolutePath() + " does not exist"); File imgFile = new File(folder.toString() + File.separatorChar + "img" + File.separatorChar + "glyphicons-halflings.png"); Assertions.assertTrue(imgFile.exists(), () -> "File " + imgFile.getAbsolutePath() + " does not exist"); File jsFile = new File(folder.toString() + File.separatorChar + "js" + File.separatorChar + "bootstrap.min.js"); Assertions.assertTrue(jsFile.exists(), () -> "File " + jsFile.getAbsolutePath() + " does not exist"); Assertions.assertTrue(asset.getErrors().isEmpty(), "Errors during asset copying"); }
@Test public void shouldSkipCopyingSingleFileIfDirectory() throws IOException { Asset asset = new Asset(config); File emptyDir = new File(folder.toFile(),"emptyDir"); emptyDir.mkdir(); File expectedDir = new File(fixtureDir.getCanonicalPath(), "emptyDir"); asset.copySingleFile(emptyDir); Assertions.assertFalse(expectedDir.exists(), "Directory should be skipped"); }
/** * Responsible for incremental baking, typically a single file at a time. * * @param fileToBake The file to bake */ public void bake(File fileToBake) { Asset asset = utensils.getAsset(); if(asset.isAssetFile(fileToBake)) { LOGGER.info("Baking a change to an asset [" + fileToBake.getPath() + "]"); asset.copySingleFile(fileToBake); } else { LOGGER.info("Playing it safe and running a full bake..."); bake(); } }
private void copy(File sourceFolder, File targetFolder, final FileFilter filter) { final File[] assets = sourceFolder.listFiles(filter); if (assets != null) { Arrays.sort(assets); for (File asset : assets) { final File target = new File(targetFolder, asset.getName()); if (asset.isFile()) { copyFile(asset, target); } else if (asset.isDirectory()) { copy(asset, target, filter); } } } }
/** * Copy one asset file at a time. * * @param asset The asset file to copy */ public void copySingleFile(File asset) { try { if ( !asset.isDirectory() ) { String targetPath = config.getDestinationFolder().getCanonicalPath() + File.separatorChar + assetSubPath(asset); LOGGER.info("Copying single asset file to [{}]", targetPath); copyFile(asset, new File(targetPath)); } else { LOGGER.info("Skip copying single asset file [{}]. Is a directory.", asset.getPath()); } } catch (IOException io) { LOGGER.error("Failed to copy the asset file.", io); } }
asset.copy(); asset.copyAssetsFromContent(config.getContentFolder()); errors.addAll(asset.getErrors());
@Test public void testCopyAssetsFromContent() { URL contentUrl = this.getClass().getResource("/fixture/content"); File contents = new File(contentUrl.getFile()); Asset asset = new Asset(config); asset.copyAssetsFromContent(contents); int totalFiles = countFiles(folder.toFile()); int expected = 3; Assertions.assertTrue(totalFiles == expected, () -> String.format("Number of files copied must be %d but are %d", expected, totalFiles)); File pngFile = new File(folder.toString() + File.separatorChar + "blog" + File.separatorChar + "2012/images/custom-image.png"); Assertions.assertTrue(pngFile.exists(), () -> "File " + pngFile.getAbsolutePath() + " does not exist"); File jpgFile = new File(folder.toString() + File.separatorChar + "blog" + File.separatorChar + "2013/images/custom-image.jpg"); Assertions.assertTrue(jpgFile.exists(), () -> "File " + jpgFile.getAbsolutePath() + " does not exist"); File jsonFile = new File(folder.toString() + File.separatorChar + "blog" + File.separatorChar + "2012/sample.json"); Assertions.assertTrue(jsonFile.exists(), () -> "File " + jsonFile.getAbsolutePath() + " does not exist"); Assertions.assertTrue(asset.getErrors().isEmpty(), "Errors during asset copying"); }
@Test public void testIsFileAsset() { File cssAsset = new File(config.getAssetFolder().getAbsolutePath() + File.separatorChar + "css" + File.separatorChar + "bootstrap.min.css"); Assertions.assertTrue(cssAsset.exists()); File contentFile = new File(config.getContentFolder().getAbsolutePath() + File.separatorChar + "about.html"); Assertions.assertTrue(contentFile.exists()); Asset asset = new Asset(config); Assertions.assertTrue(asset.isAssetFile(cssAsset)); Assertions.assertFalse(asset.isAssetFile(contentFile)); }
/** * Copy all files from assets folder to destination folder * read from configuration */ public void copy() { copy(config.getAssetFolder()); }
/** * Create default {@link Utensils} by a given {@link JBakeConfiguration} * @param config a {@link JBakeConfiguration} * @return a default {@link Utensils} instance */ public static Utensils createDefaultUtensils(JBakeConfiguration config) { JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(config); inspector.inspect(); Utensils utensils = new Utensils(); utensils.setConfiguration(config); ContentStore contentStore = DBUtil.createDataStore(config); utensils.setContentStore(contentStore); utensils.setCrawler(new Crawler(contentStore, config)); utensils.setRenderer(new Renderer(contentStore, config)); utensils.setAsset(new Asset(config)); return utensils; } }
asset.copy(); asset.copyAssetsFromContent(config.getContentFolder()); errors.addAll(asset.getErrors());
/** * Copy all files from supplied path. * * @param path The starting path */ public void copy(File path) { FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { return (!config.getAssetIgnoreHidden() || !file.isHidden()) && (file.isFile() || FileUtil.directoryOnlyIfNotIgnored(file)); } }; copy(path, config.getDestinationFolder(), filter); }
private void copy(File sourceFolder, File targetFolder, final FileFilter filter) { final File[] assets = sourceFolder.listFiles(filter); if (assets != null) { Arrays.sort(assets); for (File asset : assets) { final File target = new File(targetFolder, asset.getName()); if (asset.isFile()) { copyFile(asset, target); } else if (asset.isDirectory()) { copy(asset, target, filter); } } } }
/** * Copy one asset file at a time. * * @param asset The asset file to copy */ public void copySingleFile(File asset) { try { if ( !asset.isDirectory() ) { String targetPath = config.getDestinationFolder().getCanonicalPath() + File.separatorChar + assetSubPath(asset); LOGGER.info("Copying single asset file to [{}]", targetPath); copyFile(asset, new File(targetPath)); } else { LOGGER.info("Skip copying single asset file [{}]. Is a directory.", asset.getPath()); } } catch (IOException io) { LOGGER.error("Failed to copy the asset file.", io); } }
/** * Create default {@link Utensils} by a given {@link JBakeConfiguration} * @param config a {@link JBakeConfiguration} * @return a default {@link Utensils} instance */ public static Utensils createDefaultUtensils(JBakeConfiguration config) { JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(config); inspector.inspect(); Utensils utensils = new Utensils(); utensils.setConfiguration(config); ContentStore contentStore = DBUtil.createDataStore(config); utensils.setContentStore(contentStore); utensils.setCrawler(new Crawler(contentStore, config)); utensils.setRenderer(new Renderer(contentStore, config)); utensils.setAsset(new Asset(config)); return utensils; } }
@Test public void testCopyCustomFolder() throws Exception { config.setAssetFolder(new File(config.getSourceFolder(), "/media")); Asset asset = new Asset(config); asset.copy(); File favFile = new File(folder.toString() + File.separatorChar + "favicon.ico"); Assertions.assertTrue(favFile.exists(), () -> "File " + favFile.getAbsolutePath() + " does not exist"); Assertions.assertTrue(asset.getErrors().isEmpty(), "Errors during asset copying"); }
@Test public void shouldLogSkipCopyingSingleFileIfDirectory() throws IOException { Asset asset = new Asset(config); File emptyDir = new File(folder.toFile(),"emptyDir"); emptyDir.mkdir(); asset.copySingleFile(emptyDir); verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture()); LoggingEvent loggingEvent = captorLoggingEvent.getValue(); assertThat(loggingEvent.getMessage()).isEqualTo("Skip copying single asset file [{}]. Is a directory."); }
/** * Responsible for incremental baking, typically a single file at a time. * * @param fileToBake The file to bake */ public void bake(File fileToBake) { Asset asset = utensils.getAsset(); if(asset.isAssetFile(fileToBake)) { LOGGER.info("Baking a change to an asset [" + fileToBake.getPath() + "]"); asset.copySingleFile(fileToBake); } else { LOGGER.info("Playing it safe and running a full bake..."); bake(); } }
/** * Responsible for copying any asset files that exist within the content directory. * * @param path of the content directory */ public void copyAssetsFromContent(File path) { copy(path, config.getDestinationFolder(), FileUtil.getNotContentFileFilter()); }