Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CustomMakefile.cs « MonoDevelop.Autotools « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4965f364d5508393fdd231b9b3fa77ef13f89d3d (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// CustomMakefile.cs
//
// Author:
//   Lluis Sanchez Gual
//   Ankit Jain <jankit@novell.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

using MonoDevelop.Core;
using System.Text.RegularExpressions;
using MonoDevelop.Projects;
using MonoDevelop.Core.Instrumentation;

namespace MonoDevelop.Autotools
{
	public class CustomMakefile
	{
		static Counter UpdatedMakefiles = InstrumentationService.CreateCounter ("Updated Makefiles", "Project Model");
		
		string content;
		//FIXME: Improve the regex
		static string multilineMatch = @"(((?<content>.*)(?<!\\)\n)|((?<content>.*?)\s*\\\n([ \t]*(?<content>.*?)\s*\\\n)*[ \t]*(?<content>.*?)(?<!\\)\n))";
		string fileName;
		
		Dictionary<string, List<string>> varToValuesDict;
		List<string> dirtyVariables;

		public CustomMakefile (string file)
		{
			this.fileName = file;
			if (!File.Exists (file))
				throw new FileNotFoundException (file);

			StreamReader sr = new StreamReader (file);
			content = sr.ReadToEnd ();
			sr.Close ();
		}
		
		//This is absolute path
		public string FileName {
			get { return fileName; }
		}
		
		public string Content {
			get { return content; }
		}
		
		Dictionary<string, List<string>> VarToValuesDict {
			get {
				if (varToValuesDict == null)
					InitVarToValuesDict ();
				return varToValuesDict;
			}
		}

		List<string> DirtyVariables {
			get {
				if (dirtyVariables == null)
					dirtyVariables = new List<string> ();
				return dirtyVariables;
			}
		}

		static Regex varRegex = null;
		static Regex VariablesRegex {
			get {
				if (varRegex == null)
					varRegex = new Regex(@"[.|\n]*^(?<varname>[a-zA-Z_0-9]*)((?<sep>[ \t]*:?=[ \t]*$)|((?<sep>\s*:?=\s*)" +
						multilineMatch + "))", RegexOptions.Multiline | RegexOptions.Compiled);
				return varRegex;
			}
		}

		public ICollection<string> GetVariables ()
		{
			return VarToValuesDict.Keys;
		}

		string GetVariable (string var)
		{
			List<string> list = GetListVariable (var);
			if (list == null)
				return null;
			
			StringBuilder sb = new StringBuilder ();
			sb.AppendFormat ("{0} =", var);
			if (list.Count > 1) {
				sb.Append (" ");
				foreach (string s in list)
					sb.AppendFormat (" \\\n\t{0}", s);
			} else if (list.Count == 1) {
				sb.Append (" ");
				sb.Append (list [0]);
			}

			return sb.ToString ();
		}

		public List<string> GetListVariable (string var)
		{
			if (!VarToValuesDict.ContainsKey (var))
				return null;

			return VarToValuesDict [var];
		}

		public void SetListVariable (string var, List<string> val)
		{
			if (!VarToValuesDict.ContainsKey (var))
				return;

			VarToValuesDict [var] = val;
			if (!DirtyVariables.Contains (var))
				DirtyVariables.Add (var);
		}

		void InitVarToValuesDict ()
		{
			varToValuesDict = new Dictionary<string, List<string>> ();
			foreach (Match m in VariablesRegex.Matches (content)) {
				if (!m.Success)
					continue;

				string varname = m.Groups ["varname"].Value;
				if (String.IsNullOrEmpty (varname))
					continue;

				List<string> list = new List<string> ();
				foreach (Capture c in m.Groups["content"].Captures) {
					string val = c.Value.Trim ();
					if (val.Length == 0)
						continue;

					foreach (string s in val.Split (new char [] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries)) {
						if (s.Length > 0)
							list.Add (s);
					}
				}

				varToValuesDict [varname] = list;
			}
		}
		
		static Dictionary<string,Regex> targetExps = new Dictionary<string, Regex> ();
	
		public string GetTarget (string var)
		{
			//FIXME: //FILES = \
			//\tabc.cs ---> the \t is not a must.. and there can be multiple \t's
			Regex targetExp;
			if (!targetExps.TryGetValue (var, out targetExp)) {
				targetExp = new Regex(@"[.|\n]*^" + var + @"(?<sep>\s*:\s*)" + multilineMatch + @"\t" + multilineMatch, 
				                      RegexOptions.Multiline | RegexOptions.Compiled);
				targetExps [var] = targetExp;
			}
			return GetValue (var, targetExp);
		}
		
		string GetValue (string var, Regex exp)
		{
			Match match = exp.Match (content);
			if (!match.Success) return null;
			string value = "";
			foreach (Capture c in match.Groups["content"].Captures)
				value += c.Value;

			return value;
		}

		public void AppendValueToVar (string var, string val)
		{
			List<string> list = GetListVariable (var);
			if (list == null)
				ThrowMakefileVarNotFound (var);

			list.Add (val);
		}

		public void SetListVariable (string var, IEnumerable<string> list)
		{
			//Set only if the variable exists in the makefile
			if (GetVariable (var) == null)
				ThrowMakefileVarNotFound (var);

			VarToValuesDict [var] = new List<string> (list);
		}

		public void ClearVariableValue (string var)
		{
			if (GetVariable (var) == null)
				return;
				//ThrowMakefileVarNotFound (var);
			VarToValuesDict [var].Clear ();
		}

		static Dictionary<string,Regex> varExps = new Dictionary<string, Regex> ();
		
		void SaveVariable (string var)
		{
			Regex varExp;
			if (!varExps.TryGetValue (var, out varExp)) {
				varExp = new Regex(@"[.|\n]*^(?<var>" + var + @"((?<sep>\s*:?=\s*\n)|((?<sep>\s*:?=\s*)" + multilineMatch + ")))", 
				                         RegexOptions.Multiline | RegexOptions.Compiled);
				varExps [var] = varExp;
			}
			
			Match match = varExp.Match (content);
			if (!match.Success) 
				return;

			Group grp = match.Groups ["var"];
			int varLength = grp.ToString ().Trim (' ','\n').Length;
			
			//FIXME: Umm too expensive
			content = String.Concat ( 
					content.Substring (0, grp.Index),
					GetVariable (var),
					content.Substring (grp.Index + varLength));
		}

		public void Save ()
		{
			string oldContent = content;
			foreach (string var in DirtyVariables) {
				if (VarToValuesDict [var] != null)
					SaveVariable (var);
			}

			DirtyVariables.Clear ();

			if (String.Compare (oldContent, content) == 0)
				return;

			using (StreamWriter sw = new StreamWriter (fileName))
				sw.Write (content);

			UpdatedMakefiles.Inc (1);
		}
		
		void ThrowMakefileVarNotFound (string var)
		{
			throw new InvalidOperationException (GettextCatalog.GetString (
					"Makefile variable {0} not found in the file.", var));
		}

	}
}