public static String getFilePattern(File rootDir, File file) throws IOException { String relativePath = Files.getRelativePath(rootDir, file); if (relativePath.startsWith(File.separator)) { relativePath = relativePath.substring(1); } return relativePath.replace(File.separatorChar, '/'); }
/** * Copy the source {@link File} to the target {@link File}. */ public static void copy(File source, File target) throws IOException { if (!source.exists()) { throw new FileNotFoundException("Source file not found:" + source.getAbsolutePath()); } if (!target.exists() && !target.getParentFile().exists() && !target.getParentFile().mkdirs()) { throw new IOException("Can't create target directory:" + target.getParentFile().getAbsolutePath()); } FileInputStream is = new FileInputStream(source); FileOutputStream os = new FileOutputStream(target); copy(is, os); }
/** * Recursively deletes the given file whether its a file or directory returning the number * of files deleted */ public static int recursiveDelete(File file) { int answer = 0; if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (File child : files) { answer += recursiveDelete(child); } } } if (file.delete()) { answer += 1; } return answer; }
private boolean doPushVersionChange(PushVersionChangesContext context, String name, String value) throws IOException { boolean answer = false; File dir = context.getDir(); if (Files.isDirectory(dir)) { File[] files = dir.listFiles(); if (files != null) { for (File file : files) { String fileName = file.getName(); if (Files.isFile(file) && fileName.equals("Makefile") || fileName.startsWith("Makefile.")) { if (updateMakefile(context, file, name, value)) { answer = true; } } } } } return answer; }
protected boolean addPipeline(EnableFabric8Context context) throws IOException { Configuration configuration = context.getConfiguration(); if (Files.isFile(context.file("Jenkinsfile"))) { if (!overwriteJenkinsfile) { return false; if (Strings.notEmpty(pipeline)) { jenkinsfile = new File(jenkinsfileFolder, pipeline + "/Jenkinsfile"); found = Files.isFile(jenkinsfile); if (Files.isFile(jenkinsfile)) { found = true; break; String relativePath = Files.getRelativePath(jenkinsLibraryDir, jenkinsfile); configuration.info(LOG, "Adding Jenkinsfile " + Strings.trimAllPrefix(relativePath, "/")); File dir = context.getDir(); Files.copy(jenkinsfile, new File(dir, "Jenkinsfile")); return true;
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; }
byte[] bytes = Files.readBytes(iconFile); byte[] encoded = Base64Encoder.encode(bytes); String mimeType = guessMediaType(iconFile); return "data:" + mimeType + ";charset=UTF-8;base64," + new String(encoded); } else { File rootProjectFolder = getRootProjectFolder(); if (rootProjectFolder != null) { String relativePath = Files.getRelativePath(rootProjectFolder, iconSourceFile); String relativeParentPath = Files.getRelativePath(rootProjectFolder, getProject().getBasedir()); String urlPrefix = getConfig(Config.urlPrefix); if (Strings.isNullOrBlank(urlPrefix)) {
cloneDir.getParentFile().mkdirs(); if (cloneDir.exists()) { Files.recursiveDelete(cloneDir); Files.recursiveDelete(gitFolder); File to = destDir.getCanonicalFile(); LOG.info("Copying git checkout from " + from + " to " + to); Files.copy(from, to);
Set<File> files = Files.findRecursive(dir, filter); List<PipelineDTO> pipelines = new ArrayList<>(); for (File file : files) { try { String relativePath = Files.getRelativePath(dir, file); String value = Strings.stripPrefix(relativePath, "/"); String label = value; if (Files.isFile(markdownFile)) { descriptionMarkdown = IOHelpers.readFully(markdownFile); if (Files.isFile(yamlFile)) { PipelineMetadata metadata = null; try {
protected void copyFile(File src, File dest, Replacement replaceFn, boolean isSource) throws IOException { if (replaceFn != null && isSource) { String original = IOHelpers.readFully(src); String escapedContent = original; if (original.contains("${")) { String replaced = escapedContent.replaceAll(Pattern.quote("${"), "\\${D}{"); // add Velocity expression at the beginning of the result file. // Velocity is used by mvn archetype:generate escapedContent = "#set( $D = '$' )\n" + replaced; } if (original.contains("##")) { String replaced = escapedContent.replaceAll(Pattern.quote("##"), "\\${H}"); // add Velocity expression at the beginning of the result file. // Velocity is used by mvn archetype:generate escapedContent = "#set( $H = '##' )\n" + replaced; } // do additional replacement String text = replaceFn.replace(escapedContent); IOHelpers.writeFully(dest, text); } else { if (LOG.isDebugEnabled()) { LOG.warn("Not a source dir as the extension is {}", Files.getExtension(src.getName())); } Files.copy(src, dest); } }
String fileName = "icon." + Files.getFileExtension(iconRef); File outFile = new File(appBuildDir, fileName); Files.copy(in, new FileOutputStream(outFile)); log.info("Generated icon file " + outFile + " from icon reference: " + iconRef); return outFile;
private boolean hasLocalJenkinsFile(UIContext context, Project project) { File jenkinsFile = CommandHelpers.getProjectContextFile(context, project, "Jenkinsfile"); boolean hasJenkinsFile = Files.isFile(jenkinsFile); LOG.debug("Has Jenkinsfile " + hasJenkinsFile + " with file: " + jenkinsFile); return hasJenkinsFile; }
/** * Reads a {@link File} and returns a {@String}. */ public static String toString(File file, Charset charset) throws IOException { byte[] bytes = readBytes(file); if (charset != null) { return new String(bytes, charset); } else { return new String(bytes); } }
/** * Returns true if this file is a valid source file name */ protected boolean isSourceFile(File file) { String name = file.getName(); String extension = Files.getExtension(name).toLowerCase(); return sourceFileExtensions.contains(extension) || sourceFileNames.contains(name); }
previousLinePattern = Pattern.compile(previousLinePatternText); if (Files.isFile(file)) { String text = IOHelpers.readFully(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); String[] lines = text.split("\n"); Files.writeToFile(file, updatedText, StandardCharsets.UTF_8);
public static String getFileExtension(File file) { return getFileExtension(file.getName()); }
protected boolean addPipeline(EnableFabric8Context context) throws IOException { Configuration configuration = context.getConfiguration(); if (Files.isFile(context.file("Jenkinsfile"))) { if (!overwriteJenkinsfile) { return false; if (Strings.notEmpty(pipeline)) { jenkinsfile = new File(jenkinsfileFolder, pipeline + "/Jenkinsfile"); found = Files.isFile(jenkinsfile); if (Files.isFile(jenkinsfile)) { found = true; break; String relativePath = Files.getRelativePath(jenkinsLibraryDir, jenkinsfile); configuration.info(LOG, "Adding Jenkinsfile " + Strings.trimAllPrefix(relativePath, "/")); File dir = context.getDir(); Files.copy(jenkinsfile, new File(dir, "Jenkinsfile")); return true;
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; }
private boolean doPushVersionChange(PushVersionChangesContext context, String name, String value) throws IOException { boolean answer = false; File dir = context.getDir(); if (Files.isDirectory(dir)) { File[] files = dir.listFiles(); if (files != null) { for (File file : files) { String fileName = file.getName(); if (Files.isFile(file) && fileName.equals("Dockerfile") || fileName.startsWith("Dockerfile.")) { if (updateDockerfile(context, file, name, value)) { answer = true; } } } } } return answer; }
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; }