Java Path Create nio createMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir)

Here you can find the source of createMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir)

Description

create Main Class And Build File With Deps

License

Open Source License

Declaration

static void createMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Main {
    private static final String CPP_FILE_SUFFIX = ".cc";
    private static final String BUILD_FILE_NAME = "BUILD";

    static void createMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir) throws IOException {
        writeLinesToFile(dir.resolve("Main" + CPP_FILE_SUFFIX), "int main() {", "  return 0;", "}");
        appendLinesToFile(dir.resolve(BUILD_FILE_NAME), "cc_binary(", "    name = '" + targetName + "',",
                "    srcs = [ 'Main.cc' ],", deps, ")");
    }/*  w w  w  .j a  v a2 s .  co  m*/

    private static void writeLinesToFile(Path filePath, String... lines) throws IOException {
        writeOrAppendLinesToFile(false, filePath, lines);
    }

    private static void appendLinesToFile(Path filePath, String... lines) throws IOException {
        writeOrAppendLinesToFile(true, filePath, lines);
    }

    private static void writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)
            throws IOException {
        File file = filePath.toFile();
        if (!file.exists() && !file.createNewFile()) {
            return;
        }

        PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(file.toPath(), UTF_8,
                append ? new StandardOpenOption[] { CREATE, APPEND } : new StandardOpenOption[] { CREATE }));
        for (String line : lines) {
            printWriter.println(line);
        }
        printWriter.close();
    }
}

Related

  1. createIncrNonExistentFilename(final Path parent, final String prefix, final String suffix)
  2. createJarFile(Path directory, String name, Optional manifest, Class[] classesToAdd, Map filesToAdd)
  3. createJSONFileIfNotExists(Path path)
  4. createLanguageStructure(final String lang, final Path docRootFolder)
  5. createList(Iterable dirs)
  6. createOverwriteDirectory(Path path)
  7. createPath(String fileName, String subfolder)
  8. createPathComparator()
  9. createPathMatcher(String[] patterns)