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 void ProjectFinishedGenerating(Project project) { try { ThreadHelper.ThrowIfNotOnUIThread(); GeneratePLCnCLIProject(); } catch (Exception e) { try { project.DTE.Solution.Remove(project); DeleteProjectDirectory(); DeleteSolutionFolderIfEmpty(project.DTE); } catch (Exception) {} throw e; } void GeneratePLCnCLIProject() { _project = project; VCProject p = _project.Object as VCProject; //**********create new plcncli project********** //**********get project type********** VCConfiguration configuration = p.ActiveConfiguration; IVCRulePropertyStorage plcnextRule = configuration.Rules.Item(Constants.PLCnextRuleName); if (plcnextRule == null) { throw new NullReferenceException("PLCnextCommonProperties rule was not found in configuration rules collection."); } string projectType = plcnextRule.GetUnevaluatedPropertyValue("ProjectType_"); string newProjectCommand = Resources.Command_new_plmproject; List <string> newProjectArguments = new List <string> { Resources.Option_new_project_output, $"\"{_projectDirectory}\"", Resources.Option_new_project_projectNamespace, _projectNamespace }; if (!projectType.Equals(Resources.ProjectType_ConsumableLibrary)) { newProjectArguments.Add(Resources.Option_new_project_componentName); newProjectArguments.Add(_componentName); if (projectType.Equals(Resources.ProjectType_PLM)) { newProjectArguments.Add(Resources.Option_new_project_programName); newProjectArguments.Add(_programName); } } if (projectType.Equals(Resources.ProjectType_ACF)) { newProjectCommand = Resources.Command_new_acfproject; } else if (projectType.Equals(Resources.ProjectType_ConsumableLibrary)) { newProjectCommand = Resources.Command_new_consumablelibrary; } _plcncliCommunication.ExecuteCommand(newProjectCommand, null, null, newProjectArguments.ToArray()); //**********create configurations********** ProjectConfigurationManager.CreateConfigurationsForAllProjectTargets (_projectTargets.Select(t => t.GetNameFormattedForCommandLine()), project); foreach (TargetResult target in _projectTargets) { //**********set project target********** _plcncliCommunication.ExecuteCommand(Resources.Command_set_target, null, null, Resources.Option_set_target_add, Resources.Option_set_target_name, target.Name, Resources.Option_set_target_version, target.Version, Resources.Option_set_target_project, $"\"{_projectDirectory}\""); } //add project items to project IEnumerable <string> projectFiles = Directory.GetFiles(_projectDirectory, "*.*pp", SearchOption.AllDirectories) .Concat(Directory.GetFiles(_projectDirectory, "*.txt", SearchOption.AllDirectories)) .Where(f => !f.EndsWith("UndefClang.hpp")); foreach (string file in projectFiles) { project.ProjectItems.AddFromFile(file); } //**********generate code********** _plcncliCommunication.ExecuteCommand(Resources.Command_generate_code, null, null, Resources.Option_generate_code_project, $"\"{_projectDirectory}\""); ProjectInformationCommandResult projectInformation = _plcncliCommunication.ExecuteCommand(Resources.Command_get_project_information, null, typeof(ProjectInformationCommandResult), Resources.Option_get_project_information_project, $"\"{_projectDirectory}\"") as ProjectInformationCommandResult; CompilerSpecificationCommandResult compilerSpecsCommandResult = _plcncliCommunication.ExecuteCommand(Resources.Command_get_compiler_specifications, null, typeof(CompilerSpecificationCommandResult), Resources.Option_get_compiler_specifications_project, $"\"{_projectDirectory}\"") as CompilerSpecificationCommandResult; ProjectIncludesManager.SetIncludesForNewProject(p, compilerSpecsCommandResult, projectInformation); } }
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); } } }