Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fab85969a46f4081df14a92ec65b482ab100ae23 (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
/*******************************************************************************
 * Copyright (c) 2013 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.fsm.base;

import java.io.File;
import java.io.IOException;
import java.util.zip.CRC32;

import org.apache.commons.io.FileUtils;
import org.eclipse.etrice.core.genmodel.fsm.ILogger;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
import org.eclipse.xtext.util.RuntimeIOException;

import com.google.inject.Inject;


/**
 * @author Henrik Rentz-Reichert
 *
 */
public class IncrementalGenerationFileIo implements IGeneratorFileIo {

	@Inject JavaIoFileSystemAccess fileAccess;
	@Inject ILogger logger;

	private static boolean generateIncremental = false;
	
	/**
	 * @return {@code true} if incremental generation is chosen
	 */
	public static boolean isGenerateIncremental() {
		return generateIncremental;
	}

	/**
	 * @param generateIncremental select {@code true} if incremental generation is desired
	 */
	public static void setGenerateIncremental(boolean generateIncremental) {
		IncrementalGenerationFileIo.generateIncremental = generateIncremental;
	}

	/**
	 * This method computes a hash key (a {@link java.util.zip.CRC32 CRC32} value) for the file contents
	 * and compares it to the
	 * one stored in the {@code infopath} directory with an added extension ".info". If the values are equal
	 * then the file stored in the {@code infopath} is copied to {@code path} while preserving the file data.
	 * In the other case the key is stored and the file is stored to {@code infopath} and {@code path}.
	 */
	@Override
	public void generateFile(String desc, String path, String infopath, String file, CharSequence contents) {
		
		long oldCRC = 0;
		
		// use local copy to avoid inconsistencies due to concurrent change
		boolean genInc = generateIncremental;
		
		if (genInc) {
			// read old CRC value
			fileAccess.setOutputPath(infopath);
			try {
				CharSequence val = fileAccess.readTextFile(file+".info", JavaIoFileSystemAccess.DEFAULT_OUTPUT);
				oldCRC = Long.parseLong(val.toString());
			}
			catch (RuntimeIOException e) {
			}
			catch (NumberFormatException e) {
			}
		}
		
		boolean write = true;
		long newCRC = 0;
		
		if (genInc) {
			CRC32 crc32 = new CRC32();
			crc32.update(contents.toString().getBytes());
			newCRC = crc32.getValue();
			if (oldCRC==newCRC) {
				write = false;
			}
			else {
				fileAccess.generateFile(file+".info", ""+newCRC);
			}
		}
		
		if (write) {
			logger.logInfo(desc+" '"+file+"' in '"+path+"'");
			fileAccess.setOutputPath(path);
			fileAccess.generateFile(file, contents);
			
			if (genInc) {
				// save a copy in the info directory which is not cleared (and not compiled)
				fileAccess.setOutputPath(infopath);
				fileAccess.generateFile(file + ".incgen.txt", contents);
			}
		}
		else {
			logger.logInfo(desc+" (unchanged) '"+file+"' in '"+path+"'");
			File src = new File(infopath+file + ".incgen.txt");
			File dst = new File(path+file);
			try {
				FileUtils.copyFile(src, dst, true);
			}
			catch (IOException e) {
				fileAccess.setOutputPath(path);
				fileAccess.generateFile(file, contents);
			}
		}
	}
}

Back to the top