use of org.eclipse.n4js.cli.N4jscException in project n4js by eclipse.
the class N4jscCompiler method start.
/**
* Starts the compiler in a blocking fashion
*/
public N4jscExitState start() throws Exception {
InitializeParams params = new InitializeParams();
File baseDir = options.getDir();
if (baseDir == null) {
throw new N4jscException(N4jscExitCode.ARGUMENT_DIRS_INVALID, "No base directory");
}
params.setWorkspaceFolders(Collections.singletonList(new WorkspaceFolder(baseDir.toURI().toString())));
languageServer.initialize(params).get();
throwIfNoProjectsFound();
verbosePrintAllProjects();
switch(options.getGoal()) {
case clean:
performClean();
break;
case compile:
performCompile();
break;
default:
break;
}
languageServer.shutdown();
languageServer.exit();
return determineExitState();
}
use of org.eclipse.n4js.cli.N4jscException in project n4js by eclipse.
the class N4jscInit method initProject.
private static N4jscExitState initProject(N4jscOptions options, InitConfiguration config) throws N4jscException {
try {
config.projectRoot.resolve(config.projectFolderSrc).toFile().mkdirs();
config.projectRoot.resolve(config.projectFolderOutput).toFile().mkdirs();
config.packageJson.write(options, config.projectRoot);
for (ExampleFile exampleFile : config.files) {
exampleFile.writeToDisk(config.projectRoot);
}
} catch (IOException e) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, e);
}
return N4jscExitState.SUCCESS;
}
use of org.eclipse.n4js.cli.N4jscException in project n4js by eclipse.
the class N4jscInit method checkAndGetWorkingDirState.
private static WorkingDirState checkAndGetWorkingDirState(N4jscOptions options, File parentPackageJson) throws N4jscException {
Path cwd = options.getWorkingDirectory();
boolean cwdHasPackageJson = parentPackageJson != null && parentPackageJson.exists() && parentPackageJson.getParentFile().equals(cwd.toFile());
if (options.isN4JS()) {
if (cwdHasPackageJson) {
return WorkingDirState.InExistingProject;
} else {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Given option --n4js requires a package.json file to be in the current working directory.");
}
}
if (!options.isCreate()) {
if (cwdHasPackageJson) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Current working directory must not contain a package.json file. Note:" + NL + " In case you like to add the n4js property to an existing project, use option --n4js." + NL + " In case you like to add a project to an existing workspace project, use options -w -c.");
}
if (options.isScope() && !cwd.getParent().toFile().getName().startsWith("@")) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "When creating a scoped package the parent directory of current working directory must start with '@'. Note:" + NL + " In case you like to create a new project in a subfolder of the current working directory, use option -c.");
}
}
if (parentPackageJson == null || !parentPackageJson.exists()) {
return WorkingDirState.InEmptyFolder;
}
try (JsonReader jReader = new JsonReader(new FileReader(parentPackageJson))) {
JsonElement packageJsonCandidate = JsonParser.parseReader(jReader);
if (!packageJsonCandidate.isJsonObject()) {
return WorkingDirState.InEmptyFolder;
}
JsonObject packageJson = (JsonObject) packageJsonCandidate;
boolean isYarnProject = packageJson.has(PackageJsonProperties.WORKSPACES_ARRAY.name);
if (!isYarnProject) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Current working directory is inside the non-yarn project of " + parentPackageJson);
}
} catch (N4jscException e) {
throw e;
} catch (Exception e) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Working directory must be either empty or inside a yarn project.", e);
}
if (!options.isWorkspaces()) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Creating a new project inside an existing yarn project requires option '--workspaces' to be set.");
}
Path candidateWorkDir = parentPackageJson.getParentFile().toPath();
if (candidateWorkDir.equals(cwd)) {
return WorkingDirState.InYarnProjectRoot;
}
YarnPackageJsonContents yarnPackageJson = YarnPackageJsonContents.read(candidateWorkDir);
Path yarnRoot = parentPackageJson.getParentFile().toPath();
boolean isCwdWorkspaceMatch = workspaceMatch(yarnPackageJson.workspaces, yarnRoot, cwd);
if (!options.isCreate() && !isCwdWorkspaceMatch) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Creating a new project inside a yarn project requires the current working directory to " + "be inside a new project folder of a valid workspaces directory of the yarn project. " + "Alternatively add option '--create' to create a new project directory.");
}
if (isCwdWorkspaceMatch) {
return WorkingDirState.InYarnProjectEmptyPackage;
}
boolean isCwdWorkspaceParent = workspaceMatch(yarnPackageJson.workspaces, yarnRoot, cwd.resolve("test"));
if (isCwdWorkspaceParent) {
return WorkingDirState.InYarnProjectWorkspaces;
}
return WorkingDirState.InYarnProject;
}
use of org.eclipse.n4js.cli.N4jscException in project n4js by eclipse.
the class N4jscInit method initYarnProject.
private static N4jscExitState initYarnProject(InitConfiguration config) throws N4jscException {
config.workspacesDir.toFile().mkdirs();
Gson gson = JsonUtils.createGson();
String yarnJsonString = gson.toJson(config.yarnPackageJson);
try (FileWriter fw = new FileWriter(config.yarnRoot.resolve(N4JSGlobals.PACKAGE_JSON).toFile())) {
fw.write(yarnJsonString);
} catch (IOException e) {
throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, e);
}
return N4jscExitState.SUCCESS;
}
use of org.eclipse.n4js.cli.N4jscException in project n4js by eclipse.
the class N4jscInit method start.
/**
* Starts the compiler for goal INIT in a blocking fashion
*/
public static N4jscExitState start(N4jscOptions options) throws N4jscException {
File parentPackageJson = getParentPackageJson(options);
WorkingDirState workingDirState = checkAndGetWorkingDirState(options, parentPackageJson);
InitConfiguration config = InitDialog.getInitConfiguration(options, parentPackageJson, workingDirState);
setDirectories(options, config, parentPackageJson, workingDirState);
if (!options.isYes() && options.getAnswers() == null) {
String verb1 = options.isN4JS() ? "MODIFICATIONS" : "CONFIGURATION";
String verb2 = options.isN4JS() ? "Modify" : "Create";
N4jscConsole.print(NL);
N4jscConsole.print("PENDING PROJECT " + verb1 + NL + NL + config.toString());
N4jscConsole.print(NL + verb2 + " this project? (yes) ");
String userInput = N4jscConsole.readLine();
if (!Strings.nullToEmpty(userInput).isBlank() && !InitDialog.isYes(userInput)) {
throw new N4jscException(N4jscExitCode.USER_CANCELLED, "User said '" + userInput + "'");
}
}
if (config.isWorkspaces()) {
initYarnProject(config);
}
initProject(options, config);
String cmd = config.isWorkspaces() ? "yarn" : "npm";
println("");
println("Init done. Please run '" + cmd + " install' to install dependencies.");
println("");
println("The following scripts are available:");
println(" '" + cmd + " run n4jsc [-- args]' - run n4jsc with arguments. E.g. 'npm run n4jsc -- --help'");
println(" '" + cmd + " run build' - build this project with N4JS compiler.");
if (config.hasScript("test")) {
println(" '" + cmd + " run test' - execute project tests.");
}
return N4jscExitState.SUCCESS;
}
Aggregations