public void CreateFileWithinProject([NotNull] TestCopProjectItem projectItem, [NotNull] string targetFile)
        {
            var desiredTemplateName = LookupTemplateName(projectItem.Project);
            var boundSettingsStore  = _settingsStore.BindToContextTransient(ContextRange.ApplicationWide);

            var context = _dataContexts.CreateOnActiveControl(_lifetime);

            var applicableFileTemplateScopes = _scopeManager.EnumerateRealScopePoints(new TemplateAcceptanceContext(new ProjectFolderWithLocation(projectItem.Project)));

            applicableFileTemplateScopes = applicableFileTemplateScopes.Distinct().Where(s => s is InLanguageSpecificProject).ToList();

            var classTemplate = _storedTemplatesProvider.EnumerateTemplates(boundSettingsStore, TemplateApplicability.File)
                                .Where(x => x.Description == desiredTemplateName &&
                                       TemplateScopeManager.TemplateIsAvailable(x, applicableFileTemplateScopes))
                                .Select(x => x)
                                .FirstOrDefault();

            if (classTemplate == null)
            {
                ResharperHelper.AppendLineToOutputWindow(string.Format("File Template for '{0}' not found with default to 'Class'", desiredTemplateName));
                classTemplate = LoadTemplateFromQuickList(context, "Class");
            }
            IProjectFolder folder = (IProjectFolder)projectItem.Project.FindProjectItemByLocation(projectItem.SubNamespaceFolder)
                                    ?? GetOrCreateProjectFolder(projectItem);

            if (folder == null)
            {
                ResharperHelper.AppendLineToOutputWindow("Error failed to create/location project folder" + projectItem.SubNamespaceFolder);
                return;
            }

            string extension = Enumerable.First(_fileExtensions.GetExtensions(projectItem.Project.ProjectProperties.DefaultLanguage.DefaultProjectFileType));

            FileTemplatesManager.Instance.CreateFileFromTemplate(targetFile + extension, new ProjectFolderWithLocation(folder), classTemplate);
        }
Exemple #2
0
        //------------------------------------------------------------------------------------------------------------------------
        public static bool DeriveRelatedFileNameAndAddCreateMenus(IDataContext context, Lifetime lifetime,
                                                                  IProject project, IList <TestCopProjectItem> associatedTargetProjects, IList <SimpleMenuItem> currentMenus,
                                                                  IClrTypeName clrTypeClassName)
        {
            bool addedCreateMenuItem = false;

            if (clrTypeClassName == null)
            {
                return(false);
            }
            var baseFileName = ResharperHelper.GetBaseFileName(context, project.GetSolution());

            var  settings = TestCopSettingsManager.Instance.Settings;
            bool currentFileisTestFile = baseFileName.EndsWith(settings.TestClassSuffixes());

            foreach (var testClassSuffix in settings.GetAppropriateTestClassSuffixes(baseFileName))
            {
                var targetFile = ResharperHelper.UsingFileNameGetClassName(baseFileName).RemoveTrailing(testClassSuffix);

                if (!currentFileisTestFile)
                {
                    targetFile += testClassSuffix;
                }

                foreach (var associatedTargetProjectItem in associatedTargetProjects)
                {
                    if (currentFileisTestFile == associatedTargetProjectItem.Project.IsTestProject())
                    {
                        ResharperHelper.AppendLineToOutputWindow(
                            string.Format("Internal Error: Attempted to create '{0}' within project '{1}'"
                                          , targetFile, associatedTargetProjectItem.Project.Name));
                        continue;
                    }

                    string targetFileLocation = associatedTargetProjectItem.SubNamespaceFolder.FullPath + "\\" + targetFile;

                    if (!IsMenuItemPresentForFile(currentMenus, targetFileLocation))
                    {
                        currentMenus.AddRange(AddCreateFileMenuItem(lifetime, associatedTargetProjectItem, targetFile));
                        addedCreateMenuItem = true;
                    }
                }
            }

            return(addedCreateMenuItem);
        }