io.fabric8.utils.Files Java Examples

The following examples show how to use io.fabric8.utils.Files. 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 File: ForgeClientAsserts.java    From fabric8-forge with 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 #2
Source File: PopulateMavenRepositoryTest.java    From fabric8-forge with 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 #3
Source File: FileMatcher.java    From updatebot with 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 #4
Source File: AbstractDevOpsCommand.java    From fabric8-forge with 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 #5
Source File: SpringBootFabric8SetupTest.java    From fabric8-forge with 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 #6
Source File: SshGitCloneExample.java    From fabric8-forge with 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 #7
Source File: HelmUpdater.java    From updatebot with 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 #8
Source File: GitCloneWithTagExample.java    From fabric8-forge with 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 File: RepositoryResource.java    From fabric8-forge with 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 #10
Source File: RepositoryResource.java    From fabric8-forge with 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 #11
Source File: ProjectFileSystem.java    From fabric8-forge with 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;
}
 
Example #12
Source File: RepositoriesResource.java    From fabric8-forge with 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 #13
Source File: RepositoryResource.java    From fabric8-forge with 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 #14
Source File: RepositoryResource.java    From fabric8-forge with 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 #15
Source File: RepositoryResource.java    From fabric8-forge with 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 #16
Source File: RepositoryResource.java    From fabric8-forge with 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 #17
Source File: CamelXmlHelper.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public static List<ContextDto> loadCamelContext(CamelCatalog camelCatalog, UIContext context, Project project, String xmlResourceName) throws Exception {
    List<ContextDto> camelContexts = null;

    // try with src/main/resources or src/main/webapp/WEB-INF
    String xmlFileName = joinPaths("src/main/resources", xmlResourceName);
    File xmlFile = CommandHelpers.getProjectContextFile(context, project, xmlFileName);
    if (!Files.isFile(xmlFile)) {
        xmlFileName = joinPaths("src/main/webapp/", xmlResourceName);
        xmlFile = CommandHelpers.getProjectContextFile(context, project, xmlFileName);
    }
    if (Files.isFile(xmlFile)) {
        camelContexts = parseCamelContexts(camelCatalog, xmlFile);
    }
    return camelContexts;
}
 
Example #18
Source File: ProjectFileSystem.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public File cloneRepoIfNotExist(UserDetails userDetails, File projectFolder, String cloneUrl) {
    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, userDetails.getSshPrivateKey(), userDetails.getSshPublicKey(), this.remote);

    }
    return projectFolder;
}
 
Example #19
Source File: NewNodeXmlTest.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateXmlNode() throws Exception {
    File basedir = getBaseDir();
    String projectName = "example-camel-spring";
    File projectDir = new File(basedir, "target/test-projects/" + projectName);
    File projectSourceDir = new File(basedir, "src/itests/" + projectName);
    Files.recursiveDelete(projectDir);
    io.fabric8.forge.addon.utils.Files.copy(projectSourceDir, projectDir);

    System.out.println("Copied project to " + projectDir);

    Resource<?> resource = resourceFactory.create(projectDir);
    assertNotNull("Should have found a resource", resource);

    Project project = projectFactory.findProject(resource);
    assertNotNull("Should have found a project", project);

    List<ContextDto> contexts = getRoutesXml(project);
    assertFalse("Should have loaded a camelContext", contexts.isEmpty());

    // lets add a new route
    testAddRoute(project);

    // add endpoint to the route
    testAddEndpoint(project);

    // and then delete the route
    testDeleteRoute(project);
}
 
Example #20
Source File: AbstractDevOpsCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected boolean hasProjectFile(UIContext context, String fileName) {
    UISelection<Object> selection = context.getSelection();
    if (selection != null) {
        Object object = selection.get();
        if (object instanceof Resource) {
            File folder = ResourceUtil.getContextFile((Resource<?>) object);
            if (folder != null && Files.isDirectory(folder)) {
                File file = new File(folder, fileName);
                return file != null && file.exists() && file.isFile();
            }
        }
    }
    return false;
}
 
Example #21
Source File: AbstractDevOpsCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected File getSelectionFolder(UIContext context) {
    UISelection<Object> selection = context.getSelection();
    if (selection != null) {
        Object object = selection.get();
        if (object instanceof Resource) {
            File folder = ResourceUtil.getContextFile((Resource<?>) object);
            if (folder != null && Files.isDirectory(folder)) {
                return folder;
            }
        }
    }
    return null;
}
 
Example #22
Source File: NodeTest.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
@Test
public void testNodes() throws Exception {
    File jsonFile = new File(getBaseDir(), "src/test/resources/io/fabric8/forge/camel/dto/nodes.json");
    assertTrue("Could not find file " +jsonFile, Files.isFile(jsonFile));

    List<ContextDto> contexts = NodeDtos.parseContexts(jsonFile);
    assertFalse("Should have loaded a camelContext", contexts.isEmpty());

    List<NodeDto> nodeList = NodeDtos.toNodeList(contexts);
    assertFalse("Should have created a not empty node list", nodeList.isEmpty());

    for (NodeDto node : nodeList) {
        System.out.println(node.getLabel());
    }
}
 
Example #23
Source File: PopulateMavenRepositoryTest.java    From fabric8-forge with 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 #24
Source File: PushSourceChanges.java    From updatebot with 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 #25
Source File: LocalRepository.java    From updatebot with 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 #26
Source File: ProcessHelper.java    From updatebot with 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 #27
Source File: FileHelper.java    From updatebot with 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 #28
Source File: HelmUpdater.java    From updatebot with 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 #29
Source File: MavenUpdater.java    From updatebot with 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 #30
Source File: MavenUpdater.java    From updatebot with 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;

}