Java Code Examples for org.eclipse.xtext.generator.IFileSystemAccess#generateFile()

The following examples show how to use org.eclipse.xtext.generator.IFileSystemAccess#generateFile() . 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: MyGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	TreeIterator<EObject> allContents = input.getAllContents();
	while (allContents.hasNext()) {
		EObject next = allContents.next();
		if (next instanceof Element) {
			Element ele = (Element) next;
			String fileName = ele.getName() + ".txt";
			if (fsa instanceof IFileSystemAccess2) {
				IFileSystemAccess2 fileSystemAccess2 = (IFileSystemAccess2) fsa;
				if (fileSystemAccess2.isFile(fileName)) {
					fileSystemAccess2.readTextFile(fileName);
				}
			}
			fsa.generateFile(fileName, "object " + ele.getName());
		}
	}
}
 
Example 2
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected void _internalDoGenerate(JvmDeclaredType type, IFileSystemAccess fsa) {
	if (DisableCodeGenerationAdapter.isDisabled(type)) {
		return;
	}
	final String qn = type.getQualifiedName();
	if (!Strings.isEmpty(qn)) {
		final String fn = qn.replace('.', '/') + ".java"; //$NON-NLS-1$
		final CharSequence content = generateType(type, this.generatorConfigProvider.get(type));
		final String outputConfigurationName;
		final Boolean isTest = this.resourceTypeDetector.isTestResource(type.eResource());
		if (isTest != null && isTest.booleanValue()) {
			outputConfigurationName = SARLConfig.TEST_OUTPUT_CONFIGURATION;
		} else {
			outputConfigurationName = IFileSystemAccess.DEFAULT_OUTPUT;
		}
		fsa.generateFile(fn, outputConfigurationName, content);
	}
}
 
Example 3
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _internalDoGenerate(final JvmDeclaredType type, final IFileSystemAccess fsa) {
  boolean _isDisabled = DisableCodeGenerationAdapter.isDisabled(type);
  if (_isDisabled) {
    return;
  }
  String _qualifiedName = type.getQualifiedName();
  boolean _tripleNotEquals = (_qualifiedName != null);
  if (_tripleNotEquals) {
    String _replace = type.getQualifiedName().replace(".", "/");
    String _plus = (_replace + ".java");
    fsa.generateFile(_plus, this.generateType(type, this.generatorConfigProvider.get(type)));
  }
}
 
Example 4
Source File: ContentAssistTestLanguageGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	if (input.getContents().isEmpty())
		return;
	EObject root = input.getContents().get(0);
	if (!(root instanceof Model))
		return;
	GenerateDirective generateDirective = ((Model) root).getGenerateDirective();
	if (generateDirective != null) {
		fsa.generateFile(generateFileName(generateDirective), generateFileContents(generateDirective));
	}
}
 
Example 5
Source File: MyGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	for (Element ele : Iterables.filter(IteratorExtensions.toIterable(input.getAllContents()),
			Element.class)) {
		String fileName = ele.getName() + ".txt";
		if (fsa instanceof IFileSystemAccess2) {
			if (((IFileSystemAccess2) fsa).isFile(fileName)) {
				((IFileSystemAccess2) fsa).readTextFile(fileName);
			}
		}
		fsa.generateFile(fileName, "object " + ele.getName());
	}
}
 
Example 6
Source File: EObjectDescriptionBasedStubGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerateStubs(IFileSystemAccess access, IResourceDescription description) {
	for (IEObjectDescription objectDesc : description.getExportedObjects()) {
		String javaStubSource = getJavaStubSource(objectDesc, description);
		if(javaStubSource != null) {
			String javaFileName = getJavaFileName(objectDesc);
			access.generateFile(javaFileName, javaStubSource);
		}
	}
}
 
Example 7
Source File: ContentAssistTestLanguageGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	if (input.getContents().isEmpty())
		return;
	EObject root = input.getContents().get(0);
	if (!(root instanceof Model))
		return;
	GenerateDirective generateDirective = ((Model) root).getGenerateDirective();
	if (generateDirective != null) {
		fsa.generateFile(generateFileName(generateDirective), generateFileContents(generateDirective));
	}
}
 
Example 8
Source File: EcmaScriptSubGenerator.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void internalDoGenerate(Resource resource, GeneratorOption[] options, IFileSystemAccess fsa) {
	if (!(resource instanceof N4JSResource)) {
		if (IN4JSProject.PACKAGE_JSON.equals(resource.getURI().lastSegment())) {
			return;
		}
		throw new IllegalArgumentException("Given resource is not an N4JSResource. " + resource);
	}
	final N4JSResource resourceCasted = (N4JSResource) resource;

	if (resourceCasted.getModule().isStaticPolyfillModule()) {
		return; // do not transpile static polyfill modules (i.e. the fillers)
	}

	/*
	 * In addition to here, check for cancellation is done also on file-emit boundaries, see fsa.generateFile().
	 */
	CancelIndicator monitor = ciExtractor.extractCancelIndicator(fsa);

	// if the transpile-conditions are all met, then transpile:
	if (shouldBeCompiled(resource, monitor)) {
		RaceDetectionHelper.log("About to compile %s", resource.getURI());
		final String compiledFileExtension = getCompiledFileExtension(resource);
		final String filename = getTargetFileName(resource, compiledFileExtension);
		final String sourceMapFileExtension = getCompiledFileSourceMapExtension(resource);
		final String sourceMapFileName = getTargetFileName(resource, sourceMapFileExtension);

		RaceDetectionHelper.log("About to write %s", filename);

		// used within the file-content to refer to sibling-file:
		final String simpleSourceMapFileName = new File(sourceMapFileName).toPath().getFileName().toString();
		final String simpleCompiledFileName = new File(filename).toPath().getFileName().toString();

		// the next two variables store the navigation-prefix to get to the sources
		final Path relativeNavigationToSrc = calculateNavigationFromOutputToSourcePath(fsa, getCompilerID(),
				resourceCasted);

		boolean createSourceMap = true;

		if (filename != null) {
			final EObject root = rootElement(resource);
			if (root != null) {
				final StringWriter buffCode = new StringWriter();

				Optional<SourceMapInfo> optSourceMapData = Optional.absent();

				if (createSourceMap) {
					SourceMapInfo sourceMapDataInstance = getTranspiler().new SourceMapInfo();
					sourceMapDataInstance.sourceMapBuff = new StringWriter();

					sourceMapDataInstance.simpleSourceMapFileName = simpleSourceMapFileName;
					sourceMapDataInstance.simpleCompiledFileName = simpleCompiledFileName;

					sourceMapDataInstance.isExplicitSourceRef = false;

					sourceMapDataInstance.n4jsFilePath = relativeNavigationToSrc
							.resolve(resourceCasted.getURI().lastSegment()).toString();

					sourceMapDataInstance.sourceMapFileExtension = sourceMapFileExtension;

					optSourceMapData = Optional.of(sourceMapDataInstance);
				}

				getTranspiler().transpile(resourceCasted, options, buffCode, optSourceMapData);

				RaceDetectionHelper.log("About to write %d chars", buffCode.getBuffer().length());

				fsa.generateFile(filename, COMPILER_ID, buffCode.toString());

				if (createSourceMap) {
					fsa.generateFile(sourceMapFileName, COMPILER_ID,
							optSourceMapData.get().sourceMapBuff.toString());
				}
			}
		}
	}
}