/////////////////////////////////////////////////////////////////////////////
        public static void AddFilesToProject( InputFile srcFile, IEnumerable<string> resultFiles, IExtSvcProvider service )
        {
            // ******
            var allPossibleFiles = GetPossibleFileNames( srcFile, resultFiles );
            var filesThatExist = DiscoverGeneratedFiles( srcFile, resultFiles );
            var filesThatDontExist = allPossibleFiles.Except( filesThatExist );

            // ******
            service.AddFilesToProject( filesThatExist );
            service.RemoveFilesFromProject( filesThatDontExist );

            //// ******
            //var sb = new StringBuilder { };
            //
            //sb.Append( "Files that are in, or have just been added to project:\r\n" );
            //foreach( var name in filesThatExist ) {
            //	sb.AppendFormat( "  {0}\r\n", name );
            //}
            //
            //sb.Append( "Files that have been removed from the project:\r\n" );
            //foreach( var name in filesThatDontExist ) {
            //	sb.AppendFormat( "  {0}\r\n", name );
            //}
            //
            //// ******
            //return sb.ToString();
        }
        /////////////////////////////////////////////////////////////////////////////
        public bool Process( InputFile input, out string result )
        {
            // ******
            result = string.Empty;

            // ******
            if( cmdLineArgs.Count < 2 ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: requires these argument: [ 'target-location-in-src-file', 'name-of-source-file'  ]" );
            }

            // ******
            var target = cmdLineArgs [ 0 ];
            if( string.IsNullOrWhiteSpace( target ) ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: target text is empty" );
                return false;
            }

            // ******
            //
            // path to file
            //
            var filePath = cmdLineArgs [ 1 ]?.Trim();
            if( string.IsNullOrEmpty( filePath ) ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: requires name for source file" );
            }

            string srcText;
            //if( !TryGetSourceFile( filePath, out srcText ) ) {
            if( !TryGetSourceFile( input, filePath, out srcText ) ) {
                return false;
            }

            if( string.IsNullOrWhiteSpace( srcText ) ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: the source file is empty" );
                return false;
            }

            // ******
            int index = srcText.IndexOf( target );
            if( index < 0 ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: could not locate target text in source file, target: \"${target}\"" );
                return false;
            }

            result = srcText.Substring( 0, index ) + input.Content + srcText.Substring( index + target.Length );

            // ******
            return true;
        }
        /////////////////////////////////////////////////////////////////////////////
        public static List<string> DiscoverGeneratedFiles( InputFile srcFile, IEnumerable<string> resultFiles )
        {
            // ******
            var possibleFilePaths = GetPossibleFileNames( srcFile, resultFiles );
            var foundFiles = new List<string> { };

            foreach( var filePath in possibleFilePaths ) {
                if( File.Exists( filePath ) ) {
                    foundFiles.Add( filePath );
                }
            }

            // ******
            return foundFiles;
        }
		/////////////////////////////////////////////////////////////////////////////

		//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;
		}
        /////////////////////////////////////////////////////////////////////////////
        static List<string> GetPossibleFileNames( InputFile srcFile, IEnumerable<string> resultFiles )
        {
            // ******
            var list = new List<string> { };
            if( null == resultFiles || 0 == resultFiles.Count() ) {
                return list;
            }

            // ******
            var path = srcFile.PathOnly;
            var fileNameOnly = srcFile.NameWithoutExt;

            foreach( var item in resultFiles ) {
                if( string.IsNullOrWhiteSpace( item ) ) {
                    continue;
                }

                // ******
                var name = '*' == item [ 0 ] ? fileNameOnly + item.Substring( 1 ) : item;
                list.Add( Path.Combine( path, name ) );
            }

            // ******
            return list;
        }
        /////////////////////////////////////////////////////////////////////////////
        protected bool TryGetSourceFile( InputFile input, string fileNameIn, out string fileContents )
        {
            // ******
            fileContents = string.Empty;
            string path = string.Empty;

            // ******
            try {
                path = GetInternalPath( fileNameIn );

                if( null == path ) {
                    var argsList = ERCommand.Substitute( new List<string> { fileNameIn }, input, null, null, null );
                    path = argsList.FirstOrDefault();
                }

                if( null == path ) {
                    var fileName = fileNameIn.Replace( '/', '\\' );
                    path = Path.IsPathRooted( fileName ) ? fileName : Path.Combine( basePath, fileName );
                }

                if( !File.Exists( path ) ) {
                    reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: could not locate file: \"{fileNameIn}\" at \"{path}\"" );
                    return false;
                }

                // ******
                fileContents = File.ReadAllText( path );
                return true;
            }
            catch( Exception ex ) {
                reporter.NotifyOfErrors( ErrorType.PrepError, $"replace: exception attempting to find/load file: \"{fileNameIn}\" at \"{path}\"\r\n{ex.Message}" );
            }

            // ******
            return false;
        }
		/////////////////////////////////////////////////////////////////////////////

		public string SubstituteAndCombine( InputFile inputFile, string srcPath, string destPath, List<string> extraArgs )
		{
			var cmdLine = Substitute( inputFile, srcPath, destPath, extraArgs ).Combine( " " );
			return cmdLine;
		}
		/////////////////////////////////////////////////////////////////////////////

		public List<string> Substitute( InputFile inputFile, string srcPath, string destPath, List<string> extraArgs )
		{
			return Substitute( CmdLine, inputFile, srcPath, destPath, extraArgs );
		}
		/////////////////////////////////////////////////////////////////////////////

		/*
$file_path The directory of the current file, e.g., C:\Files. 
$file The full path to the current file, e.g., C:\Files\Document.txt. 
$file_name The name portion of the current file, e.g., Document.txt. 
$file_extension The extension portion of the current file, e.g., txt. 
$file_base_name The name-only portion of the current file, e.g., Document. 
		*/

		public static List<string> Substitute( List<string> args, InputFile inputFile, string srcPath, string destPath, List<string> extraArgs )
		{
			var processedCmds = new List<string> { };
			var moreArgs = extraArgs?.Combine( " " ) ?? null;
			var toolRunnerDir = LibInfo.CodeBasePath + "\\";
			var assetsDir = LibInfo.CodeBasePath + $"\\{Replace.ASSETS_FOLDER}\\";

			foreach( var item in args ) {
				var arg = item;
				if( string.IsNullOrWhiteSpace( arg ) ) {
					continue;
				}

				// ******
				if( null != srcPath ) {
					arg = _trySubstitute( arg, "$in", srcPath );
				}

				if( null != destPath ) {
					arg = _trySubstitute( arg, "$out", destPath );
				}

				if( null != extraArgs ) {
					arg = _trySubstitute( arg, "$args", moreArgs );
				}

				// ******
				arg = _trySubstitute( arg, @"$\", toolRunnerDir );
				arg = _trySubstitute( arg, @"$assets:", assetsDir );

				//
				// longest first so shorter ones don't interfear
				//
				arg = _trySubstitute( arg, @"$file_base_name", inputFile.NameWithoutExt );
				arg = _trySubstitute( arg, @"$file_path", inputFile.PathOnly );
				arg = _trySubstitute( arg, @"$file_ext", inputFile.FileExtWithoutDot );
				arg = _trySubstitute( arg, @"$file", inputFile.NameWithPath );

				processedCmds.Add( arg );
			}

			// ******
			return processedCmds;
		}
Exemple #10
0
		/////////////////////////////////////////////////////////////////////////////

		public RunOne( InputFile inputFile, IExtSvcProvider service )
		{
			this.input = inputFile;
			this.service = service;
		}