Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 603943706f927fa3bfc7495259b65775a2d8e1ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.generic

import com.google.inject.Inject
import com.google.inject.Singleton
import java.io.File
import java.util.Collection
import java.util.HashSet
import java.util.Set
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.xtext.generator.JavaIoFileSystemAccess
import org.eclipse.etrice.generator.base.logging.ILogger
import org.eclipse.etrice.generator.base.AbstractGenerator
import org.eclipse.etrice.generator.base.AbstractGeneratorOptions

/**
 * A class that is used to recursively erase all folders receiving generated code
 * an to place a readme file into those folders.
 */
@Singleton
class PrepareFileSystem {
	
	@Inject extension RoomExtensions
	@Inject JavaIoFileSystemAccess fileAccess
	@Inject ILogger logger
	
	def void prepareCodeTargetPaths(Resource resource) {
		var Set<String> pathes = new HashSet<String>();
		for (e: resource.contents){
			if (e instanceof Root) {
				for (mdl : (e as Root).models) {
					val tgtpath = mdl.generationTargetPath
					if (tgtpath!==null && !tgtpath.empty)
						pathes.add(tgtpath)
				}
			}
		}
		prepare(pathes)
	}
	
	def void prepareInfoTargetPaths(Resource resource) {
		if(!AbstractGenerator.settings.get(AbstractGeneratorOptions.GEN_INCREMENTAL)) 
			return;
			
		var Set<String> pathes = new HashSet<String>();
		for (e: resource.contents){
			if (e instanceof Root) {
				for (mdl : (e as Root).models) {
					val tgtpath = mdl.generationInfoPath
					if (tgtpath!==null && !tgtpath.empty)
						pathes.add(tgtpath)
				}
			}
		}
		pathes.forEach[ path |
			fileAccess.setOutputPath(path)
			fileAccess.generateFile("readme.txt", '''
				This directory is an eTrice code generation target.
				It contains auxiliary files for the incremental generation feature.
				
				DO NOT MODIFY THIS PLACE!
			''')
		]
	}
	
	def void prepareDocTargetPaths(Resource resource) {
		var Set<String> pathes = new HashSet<String>();
		for (e: resource.contents){
			if (e instanceof Root) {
				for (mdl : (e as Root).models) {
					val tgtpath = mdl.docGenerationTargetPath
					if (tgtpath!==null && !tgtpath.empty)
						pathes.add(tgtpath)
				}
			}
		}
		prepare(pathes)
	}
	
	/**
	 * Recursively erase all folders receiving generated code
	 * and place a readme file in those folders.
	 * The folders are determined from the used models of every generator
	 * model found in the resource.
	 * 
	 * @param resource a {@link Resource}
	 */
	def void prepare(Collection<String> pathes) {
		for (path : pathes) {
			logger.logInfo("clearing "+path)
			var f = new File(path)
			f.eraseContents
			fileAccess.setOutputPath(path)
			fileAccess.generateFile("readme.txt", readmeText)
		}
	}
	
	def private void eraseContents(File f) {
		if (f.directory) {
			var children = f.listFiles
			for (child : children) {
				if (!(child.directory && child.name.equals("images"))) {
					eraseContents(child)
					child.delete
				}
			}
		}
	}
	
	def private readmeText() {
		'''
			This directory is an eTrice code generation target.
			It will be erased every time the generator is executed.
			
			DO NOT PLACE OTHER FILES HERE!
		'''
	}
}

Back to the top