<commit_before>/*
 * generated by Xtext
 */
package eu.indenica.config.runtime.generator

import com.google.inject.Inject
import eu.indenica.config.runtime.generator.extensions.ClientCodeGenerator
import eu.indenica.config.runtime.generator.extensions.CodeElementGenerator
import eu.indenica.config.runtime.generator.extensions.MavenElementGenerator
import eu.indenica.config.runtime.generator.extensions.ScaCompositeGenerator
import eu.indenica.config.runtime.runtime.CodeElement
import eu.indenica.config.runtime.runtime.Component
import java.util.logging.Logger
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.naming.IQualifiedNameProvider

class RuntimeGenerator implements IGenerator {
	private static Logger LOG = Logger::getLogger(typeof(RuntimeGenerator).canonicalName)
	@Inject extension IQualifiedNameProvider
	@Inject extension CodeElementGenerator
	@Inject extension ClientCodeGenerator
	@Inject extension ScaCompositeGenerator
	@Inject extension MavenElementGenerator
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		val srcPrefix = "src/main/"
		val javaPrefix = srcPrefix + "java/"
		val resourcePrefix = srcPrefix + "resources/"
		// Generate Java artifacts
		for(e : resource.allContents.toIterable.filter(typeof(CodeElement))) {
			LOG.info("Generating class for " + e.fullyQualifiedName)
			fsa.generateFile( 
				javaPrefix + e.fullyQualifiedName.toString("/") + ".java",
				e.compile
			)
		}
		
		for(e : resource.allContents.toIterable.filter(typeof(Component))) {
			LOG.info("Generating files for " + e.fullyQualifiedName)
			fsa.generateFile(
				javaPrefix +
				e.fullyQualifiedName.toString("/") + "ActionEvent.java",
				e.compileActionEvent
			)

			if(e.generateClient) {
				LOG.info("Generating client for " + e.name)
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "EventEmitter.java",
					e.compileClientEmitter
				)				
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "ActionReceiver.java",
					e.compileActionReceiver
				)
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "client.composite",
					e.compileClientComposite
				)
			}
		}
		
		// TODO: Generate client composite and Launcher.
		
		
		// Generate infrastructure composite
		LOG.info("Generating runtime composite...")
		fsa.generateFile(
			resourcePrefix + "runtime.composite", 
			resource.compileRuntimeComposite
		)
		
//		fsa.generateFile(
//			javaPrefix + "eu/indenica/runtime/Launcher.java",
//			compileLauncher
//		)
		
		// Generate launcher pom.xml
		LOG.info("Generating launcher pom.xml...")
		fsa.generateFile(
			"pom.xml",
			compilePom
		)
	}
	

	 
//	def compile(CompositeElement it) '''
//		«compositeHeader(name)»<commit_msg>Add indication when generator is done.<commit_after>/*
 * generated by Xtext
 */
package eu.indenica.config.runtime.generator

import com.google.inject.Inject
import eu.indenica.config.runtime.generator.extensions.ClientCodeGenerator
import eu.indenica.config.runtime.generator.extensions.CodeElementGenerator
import eu.indenica.config.runtime.generator.extensions.MavenElementGenerator
import eu.indenica.config.runtime.generator.extensions.ScaCompositeGenerator
import eu.indenica.config.runtime.runtime.CodeElement
import eu.indenica.config.runtime.runtime.Component
import java.util.logging.Logger
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.naming.IQualifiedNameProvider

class RuntimeGenerator implements IGenerator {
	private static Logger LOG = Logger::getLogger(typeof(RuntimeGenerator).canonicalName)
	@Inject extension IQualifiedNameProvider
	@Inject extension CodeElementGenerator
	@Inject extension ClientCodeGenerator
	@Inject extension ScaCompositeGenerator
	@Inject extension MavenElementGenerator
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		val srcPrefix = "src/main/"
		val javaPrefix = srcPrefix + "java/"
		val resourcePrefix = srcPrefix + "resources/"
		// Generate Java artifacts
		for(e : resource.allContents.toIterable.filter(typeof(CodeElement))) {
			LOG.info("Generating class for " + e.fullyQualifiedName)
			fsa.generateFile( 
				javaPrefix + e.fullyQualifiedName.toString("/") + ".java",
				e.compile
			)
		}
		
		for(e : resource.allContents.toIterable.filter(typeof(Component))) {
			LOG.info("Generating files for " + e.fullyQualifiedName)
			fsa.generateFile(
				javaPrefix +
				e.fullyQualifiedName.toString("/") + "ActionEvent.java",
				e.compileActionEvent
			)

			if(e.generateClient) {
				LOG.info("Generating client for " + e.name)
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "EventEmitter.java",
					e.compileClientEmitter
				)				
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "ActionReceiver.java",
					e.compileActionReceiver
				)
				fsa.generateFile(
					"clients/" + e.name + "/" + e.name + "client.composite",
					e.compileClientComposite
				)
			}
		}
		
		// TODO: Generate client composite and Launcher.
		
		
		// Generate infrastructure composite
		LOG.info("Generating runtime composite...")
		fsa.generateFile(
			resourcePrefix + "runtime.composite", 
			resource.compileRuntimeComposite
		)
		
//		fsa.generateFile(
//			javaPrefix + "eu/indenica/runtime/Launcher.java",
//			compileLauncher
//		)
		
		// Generate launcher pom.xml
		LOG.info("Generating launcher pom.xml...")
		fsa.generateFile(
			"pom.xml",
			compilePom
		)
		
		LOG.info("All done.")
	}
	

	 
//	def compile(CompositeElement it) '''
//		«compositeHeader(name)»
{'fraction_non_alphanumeric': 0.11360473208072373, 'fraction_numerical': 0.0, 'mean_word_length': 3.964594127806563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5741971', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1752, 'n_words': 315}