Ejemplo n.º 1
0
		/// <summary>
		/// Helper.
		/// </summary>
		/// <param name="resolver">The resolver.</param>
		/// <param name="hasExpanded">if set to <c>true</c> [has expanded].</param>
		/// <param name="macroName">Name of the macro.</param>
		/// <param name="macroNameToCompareWith">The macro name to compare with.</param>
		/// <param name="macroValueToReplaceWith">The macro value to replace with.</param>
		private static void CheckExpandMacro(
			IExpandFilePathMacrosResolver resolver,
			ref bool hasExpanded,
			ref string macroName,
			string macroNameToCompareWith,
			string macroValueToReplaceWith )
		{
			if ( !hasExpanded && macroName != null && macroName.Length > 0 )
			{
				if ( string.Compare( macroName, macroNameToCompareWith, true ) == 0 )
				{
					if ( resolver != null )
					{
						resolver.Resolve(
							macroName,
							ref macroValueToReplaceWith );
					}

					macroName = macroValueToReplaceWith;
					hasExpanded = true;
				}
			}
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Expand "${xxx}"-placeholders.
		/// Returns original string with expanded macros (if any).
		/// If a macro could not be expanded, it is replaced by an empty string.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="resolver">The resolver.</param>
		/// <returns></returns>
		private static string ExpandMacroInString(
			string text,
			IExpandFilePathMacrosResolver resolver )
		{
			if ( string.IsNullOrEmpty( text ) || text.IndexOf( @"${" ) < 0 )
			{
				return text;
			}
			else
			{
				// First pass, "${Xxx}".
				MatchCollection matches = Regex.Matches(
					text,
					@"\$\{[^}]+\}",
					RegexOptions.IgnoreCase | RegexOptions.Compiled );

				if ( matches != null && matches.Count > 0 )
				{
					foreach ( Match match in matches )
					{
						text = text.Replace(
							match.Value,
							ExpandMacro( match.Value, resolver ) );
					}
				}

				// --

				// Second pass, "$(Xxx)".
				matches = Regex.Matches(
					text,
					@"\$\([^)]+\)",
					RegexOptions.IgnoreCase | RegexOptions.Compiled );

				if ( matches != null && matches.Count > 0 )
				{
					foreach ( Match match in matches )
					{
						text = text.Replace(
							match.Value,
							ExpandMacro( match.Value, resolver ) );
					}
				}

				// --

				return text;
			}
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Expand "${xxx}"-placeholders.
		/// Returns empty string if not found.
		/// </summary>
		/// <param name="macroName">Name of the macro.</param>
		/// <param name="resolver">The resolver.</param>
		/// <returns></returns>
		private static string ExpandMacro(
			string macroName,
			IExpandFilePathMacrosResolver resolver )
		{
			if ( macroName != null && macroName.Length > 0 )
			{
				// Remove the placeholder-surroundings.
				macroName = macroName.Trim( '$', '{', '}' );

				bool hasExpanded = false;

				// Special folders.
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.AppData",
					Environment.GetFolderPath(
					Environment.SpecialFolder.ApplicationData ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.CommonAppData",
					Environment.GetFolderPath(
					Environment.SpecialFolder.CommonApplicationData ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.Cookies",
					Environment.GetFolderPath(
					Environment.SpecialFolder.Cookies ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.History",
					Environment.GetFolderPath(
					Environment.SpecialFolder.History ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.InternetCache",
					Environment.GetFolderPath(
					Environment.SpecialFolder.InternetCache ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.LocalAppData",
					Environment.GetFolderPath(
					Environment.SpecialFolder.LocalApplicationData ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.MyPictures",
					Environment.GetFolderPath(
					Environment.SpecialFolder.MyPictures ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.Personal",
					Environment.GetFolderPath(
					Environment.SpecialFolder.Personal ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.ProgramFiles",
					Environment.GetFolderPath(
					Environment.SpecialFolder.ProgramFiles ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.ProgramFilesCommon",
					Environment.GetFolderPath(
					Environment.SpecialFolder.CommonProgramFiles ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.System",
					Environment.GetFolderPath(
					Environment.SpecialFolder.System ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.Temporary",
					Path.GetTempPath().TrimEnd( '\\' ) );

				// Assembly.
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.CallingAssembly",
					Assembly.GetCallingAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetCallingAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.EntryAssembly",
					Assembly.GetEntryAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetEntryAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.ExecutingAssembly",
					Assembly.GetExecutingAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetExecutingAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.CallingModule",
					Assembly.GetCallingAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetCallingAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.EntryModule",
					Assembly.GetEntryAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetEntryAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.ExecutingModule",
					Assembly.GetExecutingAssembly() == null ?
					string.Empty :
					Path.GetDirectoryName(
					Assembly.GetExecutingAssembly().Location ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFolder.ConfigurationFilePath",
					LogCentral.Current.ConfigurationFilePath == null ?
					string.Empty :
					Path.GetDirectoryName(
					LogCentral.Current.ConfigurationFilePath ) );

				// Assembly.
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.CallingAssembly",
					Assembly.GetCallingAssembly() == null ?
					string.Empty :
					Assembly.GetCallingAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.EntryAssembly",
					Assembly.GetEntryAssembly() == null ?
					string.Empty :
					Assembly.GetEntryAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.ExecutingAssembly",
					Assembly.GetExecutingAssembly() == null ?
					string.Empty :
					Assembly.GetExecutingAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.CallingModule",
					Assembly.GetCallingAssembly() == null ?
					string.Empty :
					Assembly.GetCallingAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.EntryModule",
					Assembly.GetEntryAssembly() == null ?
					string.Empty :
					Assembly.GetEntryAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.ExecutingModule",
					Assembly.GetExecutingAssembly() == null ?
					string.Empty :
					Assembly.GetExecutingAssembly().Location );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"SpecialFile.ConfigurationFilePath",
					LogCentral.Current.ConfigurationFilePath == null ?
					string.Empty :
					LogCentral.Current.ConfigurationFilePath );

				// Date specific.
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.Now",
					DateTime.Now.ToString( @"yyyy-MM-dd HH_mm_ss" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowDate",
					DateTime.Now.ToString( @"yyyy-MM-dd" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowTime",
					DateTime.Now.ToString( @"HH_mm_ss" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowYear",
					DateTime.Now.ToString( @"yyyy" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowMonth",
					DateTime.Now.ToString( @"MM" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowDay",
					DateTime.Now.ToString( @"dd" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowHour",
					DateTime.Now.ToString( @"HH" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowMinute",
					DateTime.Now.ToString( @"mm" ) );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Date.NowSecond",
					DateTime.Now.ToString( @"ss" ) );

				// Environment variables.
				CheckExpandMacro(
					resolver,
					ref hasExpanded, ref macroName,
					@"Environment.CurrentDirectory",
					Environment.CurrentDirectory );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.SystemDirectory",
					Environment.SystemDirectory );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.MachineName",
					Environment.MachineName );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.UserDomainName",
					Environment.UserDomainName );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.UserName",
					Environment.UserName );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.Version",
					Environment.Version.ToString() );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.OSVersion",
					Environment.OSVersion.ToString() );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.ProcessorCount",
					Environment.ProcessorCount.ToString() );
				CheckExpandMacro(
					resolver,
					ref hasExpanded,
					ref macroName,
					@"Environment.TickCount",
					Environment.TickCount.ToString() );

				if ( !hasExpanded && !string.IsNullOrEmpty( macroName ) )
				{
					CheckExpandMacro(
						resolver,
						ref hasExpanded,
						ref macroName,
						macroName,
						string.IsNullOrEmpty( macroName ) ?
						string.Empty :
						Environment.GetEnvironmentVariable( macroName ) );
				}
				if ( !hasExpanded && !string.IsNullOrEmpty( macroName ) )
				{
					CheckExpandMacro(
						resolver,
						ref hasExpanded,
						ref macroName,
						@"Environment.Variable." +
						(string.IsNullOrEmpty( macroName ) ? string.Empty : macroName),
						string.IsNullOrEmpty( macroName ) ?
						string.Empty :
						Environment.GetEnvironmentVariable( macroName ) );
				}
			}

			if ( macroName == null )
			{
				macroName = string.Empty;
			}
			return macroName;
		}
Ejemplo n.º 4
0
		// ------------------------------------------------------------------
		#endregion

		#region Configuration helper.
		// ------------------------------------------------------------------

		/// <summary>
		/// Helper function for centralized expanding of "${xxx}"-placeholders.
		/// </summary>
		/// <param name="input">The input.</param>
		/// <param name="resolver">The resolver.</param>
		/// <returns></returns>
		public static string ExpandFilePathMacros(
			string input,
			IExpandFilePathMacrosResolver resolver )
		{
			return ExpandMacroInString( input, resolver );
		}