use of org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator in project ceylon by eclipse.
the class JsCompiler method finish.
/**
* Closes all output writers and puts resulting artifacts in the output repo.
*/
protected int finish() throws IOException {
int result = 0;
String outDir = CeylonUtils.resolveRepoUrl(opts.getOutRepo());
if (!isURL(outDir)) {
File root = new File(outDir);
if (root.exists()) {
if (!(root.isDirectory() && root.canWrite())) {
logger.error("Cannot write to " + root + ". Stop.");
result = 1;
}
} else {
if (!FileUtil.mkdirs(root)) {
logger.error("Cannot create " + root + ". Stop.");
result = 1;
}
}
}
for (Map.Entry<Module, JsOutput> entry : output.entrySet()) {
JsOutput jsout = entry.getValue();
if (!compilingLanguageModule) {
jsout.publishUnsharedDeclarations(names);
}
if (opts.isModulify()) {
jsout.closeWrapper();
}
String moduleName = entry.getKey().getNameAsString();
String moduleVersion = entry.getKey().getVersion();
if (opts.getDiagnosticListener() != null)
opts.getDiagnosticListener().moduleCompiled(moduleName, moduleVersion);
// Create the JS file
final File jsart = jsout.close();
final File modart = jsout.getModelFile();
if (entry.getKey().isDefaultModule()) {
logger.info("Created module " + moduleName);
} else if (!compilingLanguageModule) {
logger.info("Created module " + moduleName + "/" + moduleVersion);
}
if (compilingLanguageModule) {
ArtifactContext artifact = new ArtifactContext(null, "delete", "me", ArtifactContext.JS);
artifact.setForceOperation(true);
outRepo.putArtifact(artifact, jsart);
} else {
final ArtifactContext artifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.JS);
artifact.setForceOperation(true);
outRepo.putArtifact(artifact, jsart);
final ArtifactContext martifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.JS_MODEL);
martifact.setForceOperation(true);
outRepo.putArtifact(martifact, modart);
// js file signature
ShaSigner.signArtifact(outRepo, artifact, jsart, logger);
ShaSigner.signArtifact(outRepo, martifact, modart, logger);
// Create the src archive
if (opts.isGenerateSourceArchive()) {
ArtifactCreator sac = CeylonUtils.makeSourceArtifactCreator(outRepo, opts.getSrcDirs(), moduleName, moduleVersion, opts.hasVerboseFlag("cmr"), logger);
sac.copy(FileUtil.filesToPathList(jsout.getSources()));
}
if (resFiles != null && !resFiles.isEmpty()) {
ArtifactCreator sac = CeylonUtils.makeResourceArtifactCreator(outRepo, opts.getSrcDirs(), opts.getResourceDirs(), opts.getResourceRootName(), moduleName, moduleVersion, opts.hasVerboseFlag("cmr"), logger);
sac.copy(FileUtil.filesToPathList(filterForModule(resFiles, opts.getResourceDirs(), moduleName)));
}
if (!entry.getKey().isDefaultModule()) {
String npmdesc = new NpmDescriptorGenerator(entry.getKey(), opts.isGenerateSourceArchive(), resFiles != null && !resFiles.isEmpty()).generateDescriptor();
File npmfile = File.createTempFile("npm", "json");
try {
try (FileWriter fw = new FileWriter(npmfile)) {
fw.write(npmdesc);
}
final ArtifactContext npmArtifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.NPM_DESCRIPTOR);
npmArtifact.setForceOperation(true);
outRepo.putArtifact(npmArtifact, npmfile);
} finally {
npmfile.delete();
}
}
}
FileUtil.deleteQuietly(jsart);
if (modart != null) {
FileUtil.deleteQuietly(modart);
}
}
return result;
}
use of org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator in project ceylon by eclipse.
the class Stitcher method encodeModel.
private static int encodeModel(final File moduleFile) throws IOException {
final String name = moduleFile.getName();
final File file = new File(moduleFile.getParentFile(), name.substring(0, name.length() - 3) + ArtifactContext.JS_MODEL);
System.out.println("Generating language module compile-time model in JSON...");
TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
tcb.addSrcDirectory(clSrcDir);
TypeChecker tc = tcb.getTypeChecker();
tc.process(true);
MetamodelVisitor mmg = null;
final ErrorCollectingVisitor errVisitor = new ErrorCollectingVisitor(tc);
for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
pu.getCompilationUnit().visit(errVisitor);
if (errVisitor.getErrorCount() > 0) {
errVisitor.printErrors(false, false);
System.out.println("errors in the language module " + pu.getCompilationUnit().getLocation());
return 1;
}
if (mmg == null) {
mmg = new MetamodelVisitor(pu.getPackage().getModule());
}
pu.getCompilationUnit().visit(mmg);
}
mod = tc.getPhasedUnits().getPhasedUnits().get(0).getPackage().getModule();
try (FileWriter writer = new FileWriter(file)) {
JsCompiler.beginWrapper(writer);
writer.write("ex$.$CCMM$=");
ModelEncoder.encodeModel(mmg.getModel(), writer);
writer.write(";\n");
final JsOutput jsout = new JsOutput(mod, true) {
@Override
public Writer getWriter() throws IOException {
return writer;
}
};
jsout.outputFile(new File(LANGMOD_JS_SRC, "MODEL.js"));
JsCompiler.endWrapper(writer);
} finally {
ShaSigner.sign(file, new JsJULLogger(), true);
}
final File npmFile = new File(moduleFile.getParentFile(), ArtifactContext.NPM_DESCRIPTOR);
try (FileWriter writer = new FileWriter(npmFile)) {
String npmdesc = new NpmDescriptorGenerator(mod, true, false).generateDescriptor();
writer.write(npmdesc);
}
return 0;
}
Aggregations