Java Code Examples for io.fabric8.utils.Files

The following examples show how to use io.fabric8.utils.Files. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: updatebot   Source File: FileMatcher.java    License: Apache License 2.0 6 votes vote down vote up
private void addMatchFiles(List<File> answer, File rootDir, File file) throws IOException {
    if (file.isDirectory()) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addMatchFiles(answer, rootDir, child);
            }
        }
    } else {
        String path = Files.getRelativePath(rootDir, file);
        path = Strings.trimAllPrefix(path, "/");
        if (matchesPatterns(path, includes) && !matchesPatterns(path, excludes)) {
            answer.add(file);
        }
    }
}
 
Example 2
Source Project: updatebot   Source File: HelmUpdater.java    License: Apache License 2.0 6 votes vote down vote up
protected boolean fileExistsInDir(File dir, String fileName) {
    if (dir.isFile()) {
        return dir.getName().equals(fileName);
    } else if (dir.isDirectory()) {
        if (isFile(new File(dir, fileName))) {
            return true;
        }
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (Files.isDirectory(file)) {
                    if (fileExistsInDir(file, fileName)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example 3
Source Project: fabric8-forge   Source File: SpringBootFabric8SetupTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void removeSnapshotFabric8Artifacts() {
    File fabric8Folder = new File(localMavenRepo, "io/fabric8");
    if (Files.isDirectory(fabric8Folder)) {
        File[] artifactFolders = fabric8Folder.listFiles();
        if (artifactFolders != null) {
            for (File artifactFolder : artifactFolders) {
                File[] versionFolders = artifactFolder.listFiles();
                if (versionFolders != null) {
                    for (File versionFolder : versionFolders) {
                        if (versionFolder.getName().toUpperCase().endsWith("-SNAPSHOT")) {
                            LOG.info("Removing snapshot version from local maven repo: " + versionFolder);
                            Files.recursiveDelete(versionFolder);
                        }
                    }
                }
            }
        }
    }
}
 
Example 4
Source Project: fabric8-forge   Source File: PopulateMavenRepositoryTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void removeSnapshotFabric8Artifacts() {
    File fabric8Folder = new File(localMavenRepo, "io/fabric8");
    if (Files.isDirectory(fabric8Folder)) {
        File[] artifactFolders = fabric8Folder.listFiles();
        if (artifactFolders != null) {
            for (File artifactFolder : artifactFolders) {
                File[] versionFolders = artifactFolder.listFiles();
                if (versionFolders != null) {
                    for (File versionFolder : versionFolders) {
                        if (versionFolder.getName().toUpperCase().endsWith("-SNAPSHOT")) {
                            LOG.info("Removing snapshot version from local maven repo: " + versionFolder);
                            Files.recursiveDelete(versionFolder);
                        }
                    }
                }
            }
        }
    }
}
 
Example 5
Source Project: fabric8-forge   Source File: ForgeClientAsserts.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that we can git clone the given repository
 */
public static Git assertGitCloneRepo(String cloneUrl, File outputFolder) throws GitAPIException, IOException {
    LOG.info("Cloning git repo: " + cloneUrl + " to folder: " + outputFolder);

    Files.recursiveDelete(outputFolder);
    outputFolder.mkdirs();

    CloneCommand command = Git.cloneRepository();
    command = command.setCloneAllBranches(false).setURI(cloneUrl).setDirectory(outputFolder).setRemote("origin");

    Git git;
    try {
        git = command.call();
    } catch (Exception e) {
        LOG.error("Failed to git clone remote repo " + cloneUrl + " due: " + e.getMessage(), e);
        throw e;
    }
    return git;
}
 
Example 6
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 6 votes vote down vote up
protected CommitInfo doRemove(Git git, List<String> paths) throws Exception {
    if (paths != null && paths.size() > 0) {
        int count = 0;
        for (String path : paths) {
            File file = getRelativeFile(path);
            if (file.exists()) {
                count++;
                Files.recursiveDelete(file);
                String filePattern = getFilePattern(path);
                git.rm().addFilepattern(filePattern).call();
            }
        }
        if (count > 0) {
            CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(message);
            return createCommitInfo(commitThenPush(git, commit));
        }
    }
    return null;
}
 
Example 7
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 6 votes vote down vote up
protected FileDTO createFileDTO(File file, boolean includeContent) {
    File parentFile = file.getParentFile();
    String relativePath = null;
    try {
        relativePath = trimLeadingSlash(Files.getRelativePath(basedir, parentFile));
    } catch (IOException e) {
        LOG.warn("Failed to find relative path of " + parentFile.getPath() + ". " + e, e);
    }
    FileDTO answer = FileDTO.createFileDTO(file, relativePath, includeContent, "", false);
    String path = answer.getPath();
    if (path.equals(".git")) {
        // lets ignore the git folder!
        return null;
    }
    // TODO use the path to generate the links...
    // TODO generate the SHA
    return answer;
}
 
Example 8
Source Project: fabric8-forge   Source File: GitCloneWithTagExample.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Arguments: gitUrl gitTag");
        return;
    }

    String cloneUrl = args[0];
    String tag = args[1];

    File projectFolder = new File("target/myproject");
    if (projectFolder.exists()) {
        Files.recursiveDelete(projectFolder);
    }
    String remote = "origin";

    ProjectFileSystem.cloneRepo(projectFolder, cloneUrl, null, null, null, remote, tag);
}
 
Example 9
Source Project: fabric8-forge   Source File: SshGitCloneExample.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    if (args.length < 3) {
        System.out.println("Arguments: gitUrl privateKeyFile publicKeyFile");
        return;
    }
    String cloneUrl = args[0];
    String privateKeyPath = args[1];
    String publicKeyPath = args[2];


    File projectFolder = new File("target/myproject");
    if (projectFolder.exists()) {
        Files.recursiveDelete(projectFolder);
    }
    File sshPrivateKey = new File(privateKeyPath);
    File sshPublicKey = new File(publicKeyPath);
    String remote = "origin";
    assertThat(sshPrivateKey).exists();
    assertThat(sshPublicKey).exists();

    ProjectFileSystem.cloneRepo(projectFolder, cloneUrl, null, sshPrivateKey, sshPublicKey, remote);
}
 
Example 10
Source Project: fabric8-forge   Source File: AbstractDevOpsCommand.java    License: Apache License 2.0 6 votes vote down vote up
protected void scanProject(File file, List<GetOverviewCommand.FileProcessor> processors, ProjectOverviewDTO overview, int level, int maxLevels) {
    if (file.isFile()) {
        String name = file.getName();
        String extension = Files.getExtension(name);
        for (GetOverviewCommand.FileProcessor processor : new ArrayList<>(processors)) {
            if (processor.processes(overview, file, name, extension, level)) {
                processors.remove(processor);
            }
        }
    } else if (file.isDirectory()) {
        int newLevel = level + 1;
        if (newLevel <= maxLevels && !processors.isEmpty()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File child : files) {
                    scanProject(child, processors, overview, newLevel, maxLevels);
                }
            }
        }
    }
}
 
Example 11
Source Project: updatebot   Source File: PushSourceChanges.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void validateConfiguration(Configuration configuration) throws IOException {
    File dir = configuration.getSourceDir();
    if (this.cloneUrl == null) {
        if (dir != null) {
            if (!Files.isDirectory(dir)) {
                throw new ParameterException("Directory does not exist " + dir);
            }
            String url;
            try {
                url = GitHelpers.extractGitUrl(dir);
            } catch (IOException e) {
                throw new ParameterException("Could not find the git clone URL in " + dir + ". " + e, e);
            }
            if (url != null) {
                setCloneUrl(GitHelper.removeUsernamePassword(url));
            }
        }
    }
    validateCloneUrl();

    if (sourceRepository == null) {
        sourceRepository = findLocalRepository(configuration);
    }
    if (sourceRepository == null) {
        File sourceDir = configuration.getSourceDir();
        if (sourceDir != null) {
            GitRepository repo = new GitRepository(dir.getName());
            // TODO create a GitHubRepository if we can figure that out
            repo.setCloneUrl(getCloneUrl());
            this.sourceRepository = new LocalRepository(repo, dir);
        }
    }

    if (dir != null) {
        configuration.setSourceDir(dir);
    }
}
 
Example 12
Source Project: updatebot   Source File: LocalRepository.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a local repository from a directory.
 */
public static LocalRepository fromDirectory(Configuration configuration, File dir) throws IOException {
    LocalRepository localRepository = new LocalRepository(new GitRepository(dir.getName()), dir);
    File configFile = new File(dir, DEFAULT_CONFIG_FILE);
    if (Files.isFile(configFile)) {
        RepositoryConfig config = RepositoryConfigs.loadRepositoryConfig(configuration, DEFAULT_CONFIG_FILE, dir);
        if (config != null) {
            GitRepositoryConfig local = config.getLocal();
            if (local != null) {
                localRepository.getRepo().setRepositoryDetails(local);
            }
        }
    }
    return localRepository;
}
 
Example 13
Source Project: updatebot   Source File: ProcessHelper.java    License: Apache License 2.0 5 votes vote down vote up
protected static String loadFile(File file) {
    String output = null;
    if (Files.isFile(file)) {
        try {
            output = IOHelpers.readFully(file);
        } catch (IOException e) {
            LOG.error("Failed to load " + file + ". " + e, e);
        }
    }
    return output;
}
 
Example 14
Source Project: updatebot   Source File: FileHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getRelativePathToCurrentDir(File dir) {
    File currentDir = new File(System.getProperty("user.dir", "."));
    try {
        String relativePath = Files.getRelativePath(currentDir, dir);
        if (relativePath.startsWith("/")) {
            return relativePath.substring(1);
        }
        return relativePath;
    } catch (IOException e) {
        return dir;
    }
}
 
Example 15
Source Project: updatebot   Source File: HelmUpdater.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean pushVersionsForDir(CommandContext context, List<DependencyVersionChange> changes, File dir) throws IOException {
    File chartsFile = new File(dir, CHART_YAML);
    boolean answer = false;
    if (isFile(chartsFile)) {
        File requirementsFile = new File(dir, REQUIREMENTS_YAML);
        if (isFile(requirementsFile)) {
            Requirements requirements;
            try {
                requirements = MarkupHelper.loadYaml(requirementsFile, Requirements.class);
            } catch (IOException e) {
                throw new IOException("Failed to load chart requirements " + requirementsFile + ". " + e, e);
            }
            if (requirements != null) {
                if (applyRequirementsChanges(context, changes, requirements, requirementsFile)) {
                    answer = true;
                }
            }
        }
    } else {
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (Files.isDirectory(file)) {
                    if (pushVersionsForDir(context, changes, file)) {
                        answer = true;
                    }
                }
            }
        }
    }
    return answer;
}
 
Example 16
Source Project: updatebot   Source File: MavenUpdater.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void addVersionChangesFromSource(CommandContext context, Dependencies dependencyConfig, List<DependencyVersionChange> list) throws IOException {
    File file = context.file("pom.xml");
    if (Files.isFile(file)) {
        // lets run the maven plugin to generate the export versions file
        Configuration configuration = context.getConfiguration();
        String configFile = configuration.getConfigFile();
        File versionsFile = createVersionsYamlFile(context);
        Map<String, String> env = configuration.getMvnEnvironmentVariables();
        String mvnCommand = configuration.getMvnCommand();
        String updateBotPluginVersion = VersionHelper.updateBotVersion();
        if (ProcessHelper.runCommandAndLogOutput(context.getConfiguration(), LOG, context.getDir(), env, mvnCommand,
                "-B",
                "io.fabric8.updatebot:updatebot-maven-plugin:" + updateBotPluginVersion + ":export",
                "-DdestFile=" + versionsFile, "-DupdateBotYaml=" + configFile)) {
            if (!Files.isFile(versionsFile)) {
                LOG.warn("Should have generated the export versions file " + versionsFile);
                return;
            }

            MavenArtifactVersionChanges changes;
            try {
                changes = MarkupHelper.loadYaml(versionsFile, MavenArtifactVersionChanges.class);
            } catch (IOException e) {
                throw new IOException("Failed to load " + versionsFile + ". " + e, e);
            }
            List<MavenArtifactVersionChange> changeList = changes.getChanges();
            if (list != null) {
                for (MavenArtifactVersionChange change : changeList) {
                    list.add(change.createDependencyVersionChange());
                }

                PrintStream printStream = configuration.getPrintStream();
                if (!changeList.isEmpty() && printStream != null) {
                    printStream.println("\n");
                }
            }
        }
    }
}
 
Example 17
Source Project: updatebot   Source File: MavenUpdater.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean pushVersions(CommandContext context, List<DependencyVersionChange> changes) throws IOException {
    File file = context.file("pom.xml");
    boolean answer = false;
    if (Files.isFile(file)) {
        if (PomHelper.updatePomVersionsInPoms(context.getDir(), changes)) {
            return true;
        }
    }
    return answer;

}
 
Example 18
Source Project: updatebot   Source File: PackageJsonUpdater.java    License: Apache License 2.0 5 votes vote down vote up
protected JsonNode getJsonFile(CommandContext context, String fileName) {
    JsonNode tree = null;
    File file = context.file(fileName);
    if (Files.isFile(file)) {
        try {
            tree = MarkupHelper.loadJson(file);
        } catch (IOException e) {
            LOG.warn("Failed to parse JSON " + file + ". " + e, e);
        }
    }
    return tree;
}
 
Example 19
Source Project: updatebot   Source File: FileUpdater.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean pushVersions(CommandContext context, List<DependencyVersionChange> changes) throws IOException {
    File file = context.file("pom.xml");
    boolean answer = false;
    if (Files.isFile(file)) {
        if (PomHelper.updatePomVersionsInPoms(context.getDir(), changes)) {
            return true;
        }
    }
    return answer;

}
 
Example 20
Source Project: updatebot   Source File: NpmTests.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean generateDummyChangePackageJson(File dir, String value, String... paths) {
    File file = new File(dir, "package.json");
    if (Files.isFile(file)) {
        try {
            JsonNode jsonNode = MarkupHelper.loadJson(file);
            MarkupAssertions.assertSetValue(jsonNode, value, paths);
            MarkupHelper.savePrettyJson(file, jsonNode);
            return true;
        } catch (IOException e) {
            LOG.warn("Failed to modify " + file + " due to: " + e, e);
        }
    }
    return false;
}
 
Example 21
Source Project: updatebot   Source File: Tests.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copies the source test data files into a test data area so we can test on the files and modify them
 *
 * @return the directory of test files
 */
public static File copyPackageSources(Class<?> clazz) {
    String packagePath = getPackagePath(clazz);
    File basedir = Tests.getBasedir();
    File testDir = getTestDataDir(clazz);
    File srcDir = new File(basedir, "src/test/resources/" + packagePath);

    assertThat(srcDir).describedAs("Source test files for test " + clazz.getName()).isDirectory();

    if (testDir.exists()) {
        Files.recursiveDelete(testDir);
    }

    assertThat(testDir).describedAs("Test data dir").doesNotExist();

    testDir.mkdirs();

    try {
        Files.copy(srcDir, testDir);
    } catch (IOException e) {
        fail("Failed to copy " + srcDir + " to " + testDir + ". " + e, e);
    }

    assertThat(testDir).describedAs("Test data dir").isDirectory();
    File[] childFiles = testDir.listFiles();
    assertThat(childFiles).describedAs("Test data dir " + testDir).isNotEmpty();
    return testDir;
}
 
Example 22
Source Project: updatebot   Source File: Tests.java    License: Apache License 2.0 5 votes vote down vote up
public static String getCleanWorkDir(Class<?> clazz) {
    File testDataDir = getTestDataDir(clazz);
    System.out.println("Using workDir: " + testDataDir);
    String property = System.getProperty("updatebot.preserve.testdir", "false");
    if (property.equals("true")) {
        LOG.info("Preserving contents of " + testDataDir + " to speed up test");
    } else {
        Files.recursiveDelete(testDataDir);
    }
    return testDataDir.getPath();
}
 
Example 23
Source Project: fabric8-forge   Source File: SpringBootFabric8SetupTest.java    License: Apache License 2.0 5 votes vote down vote up
protected void createNewProject(Furnace furnace) throws Exception {
    File projectsOutputFolder = new File(baseDir, "target/createdProjects");
    Files.recursiveDelete(projectsOutputFolder);

    ProjectGenerator generator = new ProjectGenerator(furnace, projectsOutputFolder, localMavenRepo);
    generator.createNewSpringBootProject();

    removeSnapshotFabric8Artifacts();
}
 
Example 24
Source Project: fabric8-forge   Source File: PopulateMavenRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
protected void createProjects(Furnace furnace) throws Exception {
    File projectsOutputFolder = new File(baseDir, "target/createdProjects");
    Files.recursiveDelete(projectsOutputFolder);

    ProjectGenerator generator = new ProjectGenerator(furnace, projectsOutputFolder, localMavenRepo);
    File archetypeJar = generator.getArtifactJar("io.fabric8.archetypes", "archetypes-catalog", ProjectGenerator.FABRIC8_ARCHETYPE_VERSION);

    List<String> archetypes = getArchetypesFromJar(archetypeJar);
    assertThat(archetypes).describedAs("Archetypes to create").isNotEmpty();

    String testArchetype = System.getProperty(TEST_ARCHETYPE_SYSTEM_PROPERTY, "");
    if (Strings.isNotBlank(testArchetype)) {
        LOG.info("Due to system property: '" + TEST_ARCHETYPE_SYSTEM_PROPERTY + "' we will just run the test on archetype: " + testArchetype);
        generator.createProjectFromArchetype(testArchetype);
    } else {
        for (String archetype : archetypes) {
            // TODO fix failing archetypes...
            if (!archetype.startsWith("jboss-fuse-") && !archetype.startsWith("swarm")) {
                generator.createProjectFromArchetype(archetype);
            }
        }
    }

    removeSnapshotFabric8Artifacts();


    List<File> failedFolders = generator.getFailedFolders();
    if (failedFolders.size() > 0) {
        LOG.error("Failed to build all the projects!!!");
        for (File failedFolder : failedFolders) {
            LOG.error("Failed to build project: " + failedFolder.getName());
        }
    }
}
 
Example 25
Source Project: fabric8-forge   Source File: RepositoriesResource.java    License: Apache License 2.0 5 votes vote down vote up
protected File createSshKeyFile(String namespace, String sourceSecretName, String privateKeyName, String privateKey) throws IOException {
    File keyFile = null;
    if (privateKey != null) {
        String text = Base64Encoder.decode(privateKey);
        keyFile = projectFileSystem.getSecretsFolder(namespace, sourceSecretName, privateKeyName);
        Files.writeToFile(keyFile, text.getBytes());
    }
    return keyFile;
}
 
Example 26
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 5 votes vote down vote up
protected Response doRawFile(String path) throws IOException {
    final File file = getRelativeFile(path);
    if (LOG.isDebugEnabled()) {
        LOG.debug("reading file: " + file.getPath());
    }
    if (file.isDirectory()) {
        // TODO return a listing?
        Object directoryDto = null;
        return Response.ok(directoryDto).build();
    } else {
        byte[] data = Files.readBytes(file);
        return Response.ok(data).build();
    }
}
 
Example 27
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 5 votes vote down vote up
@POST
@Path("removeProject")
public Response remove() throws Exception {
    return lockManager.withLock(gitFolder, new Callable<Response>() {

        @Override
        public Response call() throws Exception {
            LOG.info("Removing clone of project at " + basedir);
            Files.recursiveDelete(basedir);
            return Response.ok(new StatusDTO(basedir.getName(), "remove project")).build();
        }
    });
}
 
Example 28
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 5 votes vote down vote up
protected CommitInfo doRemove(Git git, String path) throws Exception {
    File file = getRelativeFile(path);
    if (file.exists()) {
        Files.recursiveDelete(file);
        String filePattern = getFilePattern(path);
        git.rm().addFilepattern(filePattern).call();
        CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(message);
        return createCommitInfo(commitThenPush(git, commit));
    } else {
        return null;
    }
}
 
Example 29
Source Project: fabric8-forge   Source File: RepositoryResource.java    License: Apache License 2.0 5 votes vote down vote up
protected CommitInfo doWrite(Git git, String path, byte[] contents, PersonIdent personIdent, String commitMessage) throws Exception {
    File file = getRelativeFile(path);
    file.getParentFile().mkdirs();

    Files.writeToFile(file, contents);

    String filePattern = getFilePattern(path);
    AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
    add.call();

    CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
    RevCommit revCommit = commitThenPush(git, commit);
    return createCommitInfo(revCommit);
}
 
Example 30
Source Project: fabric8-forge   Source File: ProjectFileSystem.java    License: Apache License 2.0 5 votes vote down vote up
public File cloneOrPullRepo(UserDetails userDetails, File projectFolder, String cloneUrl, File sshPrivateKey, File sshPublicKey) {
    File gitFolder = new File(projectFolder, ".git");
    CredentialsProvider credentialsProvider = userDetails.createCredentialsProvider();
    if (!Files.isDirectory(gitFolder) || !Files.isDirectory(projectFolder)) {
        // lets clone the git repository!
        cloneRepo(projectFolder, cloneUrl, credentialsProvider, sshPrivateKey, sshPublicKey, this.remote, this.jenkinsfileLibraryGitTag);
    } else {
        doPull(gitFolder, credentialsProvider, userDetails.getBranch(), userDetails.createPersonIdent(), userDetails);
    }
    return projectFolder;
}