Workspace { public void cleanPageWorkspace() { Path pageWorkspace = workspacePathResolver.getPagesRepositoryPath(); File file = new File(pageWorkspace.toString()); Arrays.stream(file.list()).forEach(pageFolder -> { if (".metadata".equals(pageFolder)) { cleanMetadataFolder(pageWorkspace, pageFolder); return; } try { if (isPageExist(pageWorkspace, pageFolder)) { FileUtils.deleteDirectory(pageWorkspace.resolve(pageFolder).resolve("js").toFile()); } else { File f = pageWorkspace.resolve(pageFolder).toFile(); if (f.isDirectory()) { FileUtils.deleteDirectory(pageWorkspace.resolve(pageFolder).toFile()); logger.debug(String.format("Deleting folder [%s] with success", pageWorkspace.resolve(pageFolder).toString())); } } } catch (IOException e) { String error = String.format("Technical error when deleting file [%s]", pageWorkspace.resolve(pageFolder).resolve("js").toString()); logger.error(error, e); } }); } @Inject  Workspace(WorkspacePathResolver workspacePathResolver, WidgetRepository widgetRepository, WidgetFileBasedLoader widgetLoader,
                     WidgetDirectiveBuilder widgetDirectiveBuilder, ResourceLoader resourceLoader, AssetImporter<Widget> widgetAssetImporter); void initialize(); void cleanPageWorkspace();  }
@Test public void should_delete_page_reference_when_page_doesnt_exist_anymore_but_any_file_stay_on_filesystem() throws Exception { temporaryFolder.newFolderPath("pages","myPageToRemove","js"); temporaryFolder.newFilePath("pages/.gitignore"); workspace.cleanPageWorkspace(); assertThat(temporaryFolder.toPath().resolve("pages").resolve("myPageToRemove")).doesNotExist(); assertThat(temporaryFolder.toPath().resolve("pages").resolve(".gitignore")).exists(); }
@Test public void should_delete_only_js_folder_for_page_artifact_when_page_exist() throws Exception { temporaryFolder.newFolderPath("pages","myPage","js"); temporaryFolder.newFilePath("pages/myPage/myPage.json"); workspace.cleanPageWorkspace(); assertThat(temporaryFolder.toPath().resolve("pages").resolve("myPage")).exists(); assertThat(temporaryFolder.toPath().resolve("pages").resolve("myPage").resolve("js")).doesNotExist(); }
@Test public void should_keep_file_with_a_reference_on_workspace_when_cleanup_is_called() throws Exception { temporaryFolder.newFolderPath("pages",".metadata"); temporaryFolder.newFolderPath("pages","myPage"); temporaryFolder.newFilePath("pages/myPage/myPage.json"); temporaryFolder.newFilePath("pages/.metadata/.index.json"); temporaryFolder.newFilePath("pages/.metadata/myPage.json"); temporaryFolder.newFilePath("pages/.metadata/oldestPage.json"); workspace.cleanPageWorkspace(); assertThat(temporaryFolder.toPath().resolve("pages").resolve("myPage")).exists(); assertThat(temporaryFolder.toPath().resolve("pages").resolve("myPage").resolve("js")).doesNotExist(); assertThat(temporaryFolder.toPath().resolve("pages").resolve(".metadata").resolve(".index.json")).doesNotExist(); assertThat(temporaryFolder.toPath().resolve("pages").resolve(".metadata").resolve("oldestPage.json")).doesNotExist(); assertThat(temporaryFolder.toPath().resolve("pages").resolve(".metadata").resolve("myPage.json")).exists(); }