예제 #1
0
파일: Host.cs 프로젝트: jmclain/Nmp
		/////////////////////////////////////////////////////////////////////////////

		public bool ProcessParams( CmdLineParams cmds )
		{
		
			//// ******
			//for( int iCmd = 0; iCmd < cmds.Count; iCmd++ ) {
			//	string cmd;
			//	string value;
			//
			//	// ******
			//	if( cmds.GetCommand(iCmd, out cmd, out value) ) {
			//		string key = string.Empty;
			//		
			//		// ******
			//		if( null == value ) {
			//			value = string.Empty;
			//		}
			//		
			//		// ******
			//		//WriteLine( "command {0}, value {1}", cmd, value );
			//
			//		// ******
			//		switch( cmd ) {
			//			//case "REG":
			//			//	Register( REG_WHICH.PathOnly, value );
			//			//	break;
			//			//	return true;
			//			//
			//			//case "VSREG":
			//			//	Register( REG_WHICH.VSIntegration, value );
			//			//	return true;
			//			//
			//			//case "REGALL":
			//			//	Register( REG_WHICH.All, value );
			//			//	break;
			//			//	return true;
			//			//
			//			//case "out":
			//			//	outputUTF8 = false;
			//			//	outputUnicode = false;
			//			//	outputFile = Path.IsPathRooted(value) ? value : Path.Combine(Directory.GetCurrentDirectory(), value);
			//			//	break;
			//			//	
			//			//case "out8":
			//			//	outputUTF8 = true;
			//			//	outputUnicode = false;
			//			//	outputFile = Path.IsPathRooted(value) ? value : Path.Combine(Directory.GetCurrentDirectory(), value);
			//			//	break;
			//			//	
			//			//case "out16":
			//			//	outputUnicode = true;
			//			//	outputUTF8 = false;
			//			//	outputFile = Path.IsPathRooted(value) ? value : Path.Combine(Directory.GetCurrentDirectory(), value);
			//			//	break;
			//			//	
			//			//case "define":
			//			//	if( CmdLineParams.GetValuePair(value, out key, out value) ) {
			//			//		tool.AddMacroDefinition( key, value );
			//			//	}
			//			//	else {
			//			//		WriteStdError( "no macro name to define" );
			//			//	}
			//			//	break;
			//			//
			//			//case "undef":
			//			//	if( string.IsNullOrEmpty(value) ) {
			//			//		WriteStdError( "no macro name to undef" );
			//			//	}
			//			//	else {
			//			//		////WriteLine( "  undef: \"{0}\"", value );
			//			//		//defines.Remove( value );
			//			//		tool.RemoveMacroDefinition( value );
			//			//	}
			//			//	break;
			//			//
			//			//case "t":
			//			//	tool.SetParam( "traceon", null == value ? "0" : value );
			//			//	break;
			//			//	
			//			//case "p":
			//			//	noPathInWarnError = true;
			//			//	break;
			//			//	
			//			//	
			//			//case "run":
			//			//	ExecMethod( tool, value );
			//			//	break;
			//
			//			case "test":
			//			case "t":
			//				break;
			//				
			//			default:
			//				isCmdLineError = true;
			//				WriteStdError( "unknown command line switch \"{0}\"", cmd );
			//				break;
			//		}
			//	}
			//	else {
			//		//WriteLine( "value {1}", cmd, value );
			//		
			//		//// ******
			//		//string fileName = value.ToLower().Trim();
			//		//fileName = Path.IsPathRooted(fileName) ? fileName : Path.Combine( Directory.GetCurrentDirectory(), fileName );
			//		//
			//		//if( NMP_CMDFILE_EXT == Path.GetExtension(fileName) ) {
			//		//	//
			//		//	// treat as a response file
			//		//	//
			//		//	if( File.Exists(fileName) ) {
			//		//		CmdLineParams p = new CmdLineParams( fileName, false );
			//		//		cmds.InsertRange( 1 + iCmd, p );
			//		//		
			//		//		Directory.SetCurrentDirectory( Path.GetDirectoryName(fileName) );
			//		//	}
			//		//	else {
			//		//		WriteStdError( "unable to locate response file: {0}", fileName );
			//		//		return false;
			//		//	}
			//		//}
			//		//else if( LINK_FILE_EXT == Path.GetExtension(fileName) ) {
			//		//	//
			//		//	// ignore for now
			//		//	//
			//		//	WriteStdError( ".lnk files are not currently supported" );
			//		//	return false;
			//		//}
			//		//else if( File.Exists(fileName) ) {
			//		//	bool appendToFile = 0 == string.Compare( outputFile, prevOutputFile, true );
			//		//	
			//		//	// ******
			//		//	if( ! ProcessFile(fileName, outputFile, appendToFile) ) {
			//		//		return false;
			//		//	}
			//		//	
			//		//	// ******
			//		//	prevOutputFile = outputFile;
			//		//}
			//		//else {
			//		//	WriteStdError( "file \"{0}\" could not be found", fileName );
			//		//	return false;
			//		//}
			//	}
			//	
			//}
			
		bool		isCmdLineError = false;
		bool		consoleOut = false;
		string	outputFile = string.Empty;
		string	prevOutputFile = string.Empty;
		//string	fileExt = "txt";
		NmpStringArray defines = new NmpStringArray { };

		Encoding encoding = Encoding.UTF8;

			// ******
			while( cmds.Count > 0 ) {
				string cmd;
				string value;
				bool isCmd = cmds.GetCommand( 0, out cmd, out value );
				cmds.Remove( 0 );
			
				// ******
				if( isCmd ) {
					string key = string.Empty;
					
					// ******
					if( null == value ) {
						value = string.Empty;
					}
					
					// ******
					//WriteLine( "command {0}, value {1}", cmd, value );
			
					// ******
					switch( cmd ) {
						//case "ext":
						//	if( ! string.IsNullOrEmpty(value) ) {
						//		fileExt = value.Trim();
						//	}
						//	break;

						//case "mc":
						//	Recognizer = new DefaultRecognizer();
						//	MacroHandler = new MPMacrosCalled( Recognizer );
						//	break;

						case "console":
						case "c":
							consoleOut = true;
							break;

						case "out":
							consoleOut = false;
							if( string.IsNullOrEmpty(value) ) {
								outputFile = string.Empty;
							}
							else {
								outputFile = value.Trim();
								if( ! Path.IsPathRooted(outputFile) ) {
									outputFile = Path.GetFullPath( outputFile );
								}
							}
							break;

						case "define":
						case "d":
							if( CmdLineParams.GetValuePair( value, out key, out value ) ) {
								defines.Add( key, value );
							}
							else {
								WriteStdError( "bad define \"{0}\", usage: -d:macro_name=\"value\"", value );
							}
							break;

						case "undef":
							if( string.IsNullOrEmpty( value ) ) {
								WriteStdError( "no macro name to undef" );
							}
							else {
								////WriteLine( "  undef: \"{0}\"", value );
								//defines.Remove( value );
								//tool.RemoveMacroDefinition( value );
								defines.Add( "-" + value, string.Empty );
							}
							break;
						

						//case "test":
						//case "t":
						//	TestCmd( value, cmds );
						//	break;
						

						case "register":
						case "r":
							Register( true );
							break;

						//case "snampshot":
						//case "s":
						//	snapShot = true;
						//	break;

						case "unregister":
						case "u":
							Register( false );
							break;


						case "ascii":
							encoding = Encoding.ASCII;
							break;

						case "utf7":
							encoding = Encoding.UTF7;
							break;

						case "utf8":
							encoding = Encoding.UTF8;
							break;

						case "utf32":
							encoding = Encoding.UTF32;
							break;

						case "unicode":
							encoding = Encoding.Unicode;
							break;


						case "trace":
						case "t":
							if( null == macroTracer ) {
								macroTracer = new BasicTracer();
							}
							break;
						
							
						default:
							Die( "unknown command line switch \"{0}\"", cmd );
							break;
					}
							
				}
				else {
					
					//WriteLine( "value {1}", cmd, value );
					
					// ******
					//string fileName = value.ToLower().Trim();
					string fileName = value.Trim();
					fileName = Path.IsPathRooted(fileName) ? fileName : Path.GetFullPath( fileName );

					// ******
					if( NMP_CMDFILE_EXT == Path.GetExtension(fileName) ) {
						//
						// .rsp - treat as a response file
						//
						if( File.Exists(fileName) ) {
							CmdLineParams p = new CmdLineParams( fileName, false );
							cmds.InsertRange( 0, p );
							
							Directory.SetCurrentDirectory( Path.GetDirectoryName(fileName) );
						}
						else {
							Die( "unable to locate response file: {0}", fileName );
						}
					}

					//else if( LINK_FILE_EXT == Path.GetExtension(fileName) ) {
					//	//
					//	// ignore for now
					//	//
					//	WriteLine( ".lnk files are not currently supported" );
					//	return false;
					//}
					
					else if( File.Exists(fileName) ) {
						//
						// use outputFile if we have one
						//
						string saveFile = ! string.IsNullOrEmpty(outputFile) ? outputFile : string.Format( @"{0}\{1}", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName) );
						
						// ******
						//
						// where
						//
						//		Item1 is the resulting text output by Nmp
						//
						//		Item2 is the file ext set by by the macro script (usually empty)
						//
						//		Item3 is the "no output" flag where the macro script requested that we not output a file
						//
						//		Item4 is output Encoding (as a string value)
						//
						Tuple<string, string, bool, string> result = EvaluateFile( fileName, defines );

						// ******
						//
						// Item3 is true if there should be no output file 
						//
						if( ! result.Item3 ) {
							if( consoleOut ) {
								Console.Write( result.Item1 );
							}
							else {
								//
								// append ext - may be empty
								//
								string path = saveFile + result.Item2;
								var encodingToUse = DetermineEncoding( encoding, result.Item4 );

								if( 0 == string.Compare( path, prevOutputFile, true ) ) {
									File.AppendAllText( path, result.Item1, encodingToUse );
								}
								else {
									File.WriteAllText( path, result.Item1, encodingToUse );
								}

								// ******
								prevOutputFile = path;
							}
						}
					}
					else {
						WriteMessage( "file \"{0}\" could not be found", fileName );
						return false;
					}


					//if( consoleOut ) {
					//}

					//else {
					//	Die( "unknown non command argument: {0}", cmd );
					//}
				}
			}

			// ******
			return ! isCmdLineError;
		}
예제 #2
0
파일: Host.cs 프로젝트: jmclain/Nmp
		/////////////////////////////////////////////////////////////////////////////

		protected Tuple<string, string, bool, string> EvaluateFile( string fileName, NmpStringArray defines )
		{
			// ******
			if( null != macroTracer ) {
				macroTracer.BeginSession( Path.GetFileName( fileName ), Path.GetDirectoryName( fileName ) );
			}

			// ******
			try {
				if( ! File.Exists(fileName) ) {
					Die( "input file does not exist: {0}", fileName );
				}

				// ******
				using( var mp = new NmpEvaluator(this) ) {

					foreach( var kvp in defines ) {
						var key = kvp.Key;
						if( !string.IsNullOrEmpty( key ) && '-' == key [ 0 ] ) {
							//
							// v3 have not added undef back to evaluator
							//
							continue;
						}

						// ******
						mp.AddTextMacro( key, kvp.Value, null );
					}

					// ******
					string result = mp.Evaluate( mp.GetFileContext(fileName), true );
					return new Tuple<string,string, bool, string>(result, mp.FileExt, mp.NoOutputFile, mp.OutputEncoding);
				}
			}

	//
	// catch error and report here
	//
#if EXREPORTING
			// ref dll
#endif

			finally {
				// ******
				if( null != macroTracer ) {
					macroTracer.EndSession();
				}

			}
		}