public static void SetIncludesForNewProject(VCProject vcProject, CompilerSpecificationCommandResult compilerSpecsCommandResult, ProjectInformationCommandResult projectInformation) { (IEnumerable <CompilerMacroResult> macros, IEnumerable <string> includes) = ProjectIncludesManager.FindMacrosAndIncludesForMinTarget(compilerSpecsCommandResult, projectInformation); if (macros == null) { macros = Enumerable.Empty <CompilerMacroResult>(); } if (includes == null) { includes = Enumerable.Empty <string>(); } foreach (VCConfiguration2 config in vcProject.Configurations) { IVCRulePropertyStorage plcnextCommonPropertiesRule = config.Rules.Item(Constants.PLCnextRuleName); if (plcnextCommonPropertiesRule == null) { MessageBox.Show("PLCnextCommonProperties rule was not found in configuration rules collection."); } string joinedMacros = macros.Any() ? string.Join(";", macros.Select(m => m.Name + (string.IsNullOrEmpty(m.Value.Trim()) ? null : "=" + m.Value))) : string.Empty; string joinedIncludes = includes.Any() ? string.Join(";", includes) : string.Empty; plcnextCommonPropertiesRule.SetPropertyValue(Constants.PLCnextMacrosKey, joinedMacros); plcnextCommonPropertiesRule.SetPropertyValue(Constants.PLCnextIncludesKey, joinedIncludes); } }
public static void UpdateIncludesAndMacrosForExistingProject(VCProject vcProject, IEnumerable <CompilerMacroResult> oldMacros, CompilerSpecificationCommandResult newCompilerSpecs, IEnumerable <string> oldIncludes, ProjectInformationCommandResult newProjectInformation) { var(newMacros, newIncludes) = ProjectIncludesManager.FindMacrosAndIncludesForMinTarget(newCompilerSpecs, newProjectInformation); if (newMacros == null) { newMacros = Enumerable.Empty <CompilerMacroResult>(); } if (newIncludes == null) { newIncludes = Enumerable.Empty <string>(); } IVCRulePropertyStorage plcnextCommonPropertiesRule = vcProject.ActiveConfiguration.Rules.Item(Constants.PLCnextRuleName); foreach (VCConfiguration2 config in vcProject.Configurations) { plcnextCommonPropertiesRule = config.Rules.Item(Constants.PLCnextRuleName); CheckIncludesVariableIsInProjectIncludes(config); CheckMacrosVariableIsInProjectMacros(config); DeleteObsoleteIncludes(); DeleteObsoleteMacros(); void DeleteObsoleteIncludes() { string savedIncludes = plcnextCommonPropertiesRule.GetUnevaluatedPropertyValue(Constants.PLCnextIncludesKey); if (!string.IsNullOrEmpty(savedIncludes) || oldIncludes == null || !oldIncludes.Any()) { return; } IVCRulePropertyStorage rule = config.Rules.Item(Constants.VCppIncludesRuleName); IEnumerable <string> currentIncludes = rule.GetUnevaluatedPropertyValue(Constants.VCppIncludesKey) .Split(';') .Where(i => !oldIncludes.Contains(i)); rule.SetPropertyValue(Constants.VCppIncludesKey, currentIncludes.Any() ? string.Join(";", currentIncludes) : string.Empty); } void DeleteObsoleteMacros() { string savedMacros = plcnextCommonPropertiesRule.GetUnevaluatedPropertyValue(Constants.PLCnextMacrosKey); if (!string.IsNullOrEmpty(savedMacros) || oldMacros == null || !oldMacros.Any()) { return; } IVCRulePropertyStorage clRule = config.Rules.Item(Constants.CLRuleName); IEnumerable <CompilerMacroResult> currentMacros = clRule.GetUnevaluatedPropertyValue(Constants.VCPreprocessorsKey) .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(m => m.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries)) .Select(m => new CompilerMacroResult { Name = m[0], Value = m.Length == 2 ? m[1].Trim() : null }); currentMacros = currentMacros.Where(m => !oldMacros.Any(y => y.Name.Equals(m.Name) && (y.Value != null ? (m.Value == null ? string.IsNullOrEmpty(y.Value.Trim()) : y.Value.Trim().Equals(m.Value.Trim())) : string.IsNullOrEmpty(m.Value?.Trim())))); IEnumerable <string> macros = currentMacros.Select(m => m.Name + (string.IsNullOrEmpty(m.Value?.Trim()) ? string.Empty : ("=" + m.Value))); clRule.SetPropertyValue(Constants.VCPreprocessorsKey, macros.Any() ? string.Join(";", macros) : string.Empty); } } plcnextCommonPropertiesRule.SetPropertyValue(Constants.PLCnextIncludesKey, newIncludes.Any() ? string.Join(";", newIncludes) : string.Empty); plcnextCommonPropertiesRule.SetPropertyValue(Constants.PLCnextMacrosKey, newMacros.Any() ? string.Join(";", newMacros.Select(m => m.Name + (string.IsNullOrEmpty(m.Value?.Trim()) ? string.Empty : ("=" + m.Value)))) : string.Empty); void CheckIncludesVariableIsInProjectIncludes(VCConfiguration2 config) { IVCRulePropertyStorage rule = config.Rules.Item(Constants.VCppIncludesRuleName); string currentIncludes = rule.GetUnevaluatedPropertyValue(Constants.VCppIncludesKey); if (!currentIncludes.Split(';').Where(i => i.Trim().Equals($"$({Constants.PLCnextIncludesKey})", StringComparison.OrdinalIgnoreCase)).Any()) { string includes = string.IsNullOrEmpty(currentIncludes) ? $"$({Constants.PLCnextIncludesKey})" : string.Join(";", currentIncludes, $"$({Constants.PLCnextIncludesKey})"); rule.SetPropertyValue(Constants.VCppIncludesKey, includes); } } void CheckMacrosVariableIsInProjectMacros(VCConfiguration2 config) { IVCRulePropertyStorage clRule = config.Rules.Item(Constants.CLRuleName); string currentMacros = clRule.GetUnevaluatedPropertyValue(Constants.VCPreprocessorsKey); if (!currentMacros.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Where(m => m.Trim() .Equals($"$({Constants.PLCnextMacrosKey})", StringComparison.OrdinalIgnoreCase)) .Any()) { string macros = string.IsNullOrEmpty(currentMacros) ? $"$({Constants.PLCnextMacrosKey})" : string.Join(";", currentMacros, $"$({Constants.PLCnextMacrosKey})"); clRule.SetPropertyValue(Constants.VCPreprocessorsKey, macros); } } }