Beispiel #1
0
 /////////////////////////////////////////////////////////////////////////////
 public Replace( IExtSvcProvider reporter, string basePath, ERCommand erCmd )
 {
     this.reporter = reporter;
     this.basePath = basePath;
     //this.erCmd = erCmd;
     this.cmdLineArgs = erCmd.CmdLine;
 }
Beispiel #2
0
		/////////////////////////////////////////////////////////////////////////////

		//string FixSrcNameInStdErr( string stdErr, string tempInputName, string actualInputName )
		//{
		//	// ******
		//	string result = stdErr;
		//	while( true ) {
		//		if( result.Contains( tempInputName ) ) {
		//			result = result.Replace( tempInputName, actualInputName );
		//		}
		//		else {
		//			return result;
		//		}
		//	}
		//}


		/////////////////////////////////////////////////////////////////////////////

		protected bool TryHandleInternalCmd( string cmdIn, ERCommand erCmd, InputFile input, out string result )
		{
			// ******
			result = string.Empty;
			var cmd = cmdIn?.Trim().ToLower();

			// ******
			if( !string.IsNullOrEmpty( cmd ) ) {
				switch( cmd.Trim().ToLower() ) {
					case "replace":
						return new Replace( service, input.PathOnly, erCmd ).Process( input, out result );
				}
			}

			// ******
			service.NotifyOfErrors( ErrorType.PrepError, $"unknown internal command: {cmdIn}" );
			return false;
		}
Beispiel #3
0
		/////////////////////////////////////////////////////////////////////////////

		public bool Run( ERCommand erCmd, out string result )
		{
			// ******
			result = null;

			if( Debugger.IsAttached && erCmd.DebugBreak ) {
				Debugger.Break();
			}

			// ******
			var execuitable = erCmd.ExecutableName;
			if( string.IsNullOrWhiteSpace( execuitable ) ) {
				service.NotifyOfErrors( ErrorType.PrepError, "no command name was provided" );
				return false;
			}

			// ******
			const string INTERNAL = "internal:";
			if( execuitable.StartsWith( INTERNAL, StringComparison.OrdinalIgnoreCase ) ) {
				return TryHandleInternalCmd( execuitable.Substring( INTERNAL.Length ), erCmd, input, out result );
			}

			// ******
			var cmdExt = Path.GetExtension( execuitable );
			//
			// should look this up in the registery
			//
			if( ".cmd" == cmdExt || ".bat" == cmdExt ) {
				execuitable = "cmd.exe";
			}
			else if( ".btm" == cmdExt ) {
				execuitable = "tcc.exe";
			}

			// ******
			bool success = false;
			var bareFileName = input.NameWithoutExt;
			var cmdInputFile = GetTempFilePath( bareFileName );
			var cmdOutputFile = GetFilePath( bareFileName, ".out.txt" );

			try {
				var proccessedInputText = ProcessOptions( input.Content );
				var commandLine = erCmd.SubstituteAndCombine( input, cmdInputFile, cmdOutputFile, new List<string> { } );

				// ******
				WriteNmpInputFile( cmdInputFile, proccessedInputText );

				// ******
				var run = new External { };
				var exeResult = run.Execute( execuitable, false, commandLine, 10000 );

				if( exeResult.Success ) {
					var exitCode = exeResult.ExitCode;

					if( ActionEnum.RunOnlyIgnoreExitCode == erCmd.ActionEnum ) {
						//
						// don't expect or handle output file, no exit code check
						//
						success = true;
					}
					else if( ActionEnum.RunOnly == erCmd.ActionEnum ) {
						//
						// don't expect or handle output file, do check exit code
						//
						success = 0 == exitCode;
					}
					else {
						//
						// expect output, do check exit code
						//
						if( 0 == exitCode ) {
							//
							// success
							//
							success = true;

							if( erCmd.ReadStdout ) {
								result = exeResult.StdOut;
							}
							else if( File.Exists( cmdOutputFile ) ) {
								result = ReadNmpOutputFile( cmdOutputFile );
							}
							else {
								result = null;
							}

							//if( success ) {
							service.NotifyOfSuccess();
							//}
						}
						else {
							//
							// failure
							//
							//var errStr = FixSrcNameInStdErr( exeResult.StdErr, cmdInputFile, input.NameWithPath );
							var errStr = exeResult.StdErr.Replace( cmdInputFile, input.NameWithPath );
							service.NotifyOfErrors( ErrorType.Error, errStr );
							success = false;
						}
					}

				}
				else {
					//
					// failed to execute cmd
					//
					service.NotifyOfErrors( ErrorType.FailureToExecute, execuitable );
					success = false;
				}

			}
			finally {

				if( !KeepTempFiles ) {
					ResultFilesHelper.DeleteFiles( new List<string> { cmdInputFile, cmdOutputFile } );
				}

			}

			// ******
			return success;
		}