コード例 #1
0
        XElement SaveToDisk(IResource resource, StringBuilder contents, string directoryName, TxFileManager fileManager)
        {
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            if (_dev2FileWrapper.Exists(resource.FilePath))
            {
                // Remove readonly attribute if it is set
                var attributes = _dev2FileWrapper.GetAttributes(resource.FilePath);
                if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    _dev2FileWrapper.SetAttributes(resource.FilePath, attributes ^ FileAttributes.ReadOnly);
                }
            }

            var xml = contents.ToXElement();

            xml = resource.UpgradeXml(xml, resource);
            var result = xml.ToStringBuilder();

            var signedXml = HostSecurityProvider.Instance.SignXml(result);

            lock (Common.GetFileLock(resource.FilePath))
            {
                signedXml.WriteToFile(resource.FilePath, Encoding.UTF8, fileManager);
            }
            return(xml);
        }
コード例 #2
0
 private void DeleteIfExists(string url)
 {
     if (FileWrapper.Exists(url))
     {
         FileWrapper.Delete(url);
     }
 }
コード例 #3
0
        public StringBuilder GetResourceContents(IResource resource)
        {
            var contents = new StringBuilder();

            if (string.IsNullOrEmpty(resource?.FilePath) || !_dev2FileWrapper.Exists(resource.FilePath))
            {
                return(contents);
            }

            // Open the file with the file share option of read. This will ensure that if the file is opened for write while this read operation
            // is happening the wite will fail.
            using (FileStream fs = new FileStream(resource.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        var readLine = sr.ReadLine();
                        if (!string.IsNullOrEmpty(readLine))
                        {
                            contents.Append(readLine);
                            contents.Append(Environment.NewLine);
                        }
                    }
                }
            }
            return(contents);
        }
コード例 #4
0
        // disclaimer: visual studio doesn't support adding dependent file under linked file
        // so no dependent transformed config under linked app.config in designer
        private static void CreateConfigFiles(Project project, ProjectItem projectItem)
        {
            string appConfigName           = projectItem.Name;
            var    buildConfigurationNames = project.GetBuildConfigurationNames();
            var    projectFileIsDirty      = false;
            // get app.config directory. new transform config will be created there.
            //var path = projectItem.GetFullPath();
            string path = null;

            if (projectItem.IsLink())
            {
                path = Directory.GetParent(projectItem.ContainingProject.FullName).FullName;
            }
            else
            {
                path = Directory.GetParent(projectItem.GetFullPath()).FullName;
            }

            foreach (var buildConfigurationName in buildConfigurationNames)
            {
                var dependentConfig         = GetTransformConfigName(appConfigName, buildConfigurationName);
                var dependentConfigFullPath = Path.Combine(path, dependentConfig);
                // check if config file exist
                if (!FileWrapper.Exists(dependentConfigFullPath))
                {
                    using (var file = FileWrapper.AppendText(dependentConfigFullPath))
                    {
                        file.Write(DependencyConfigContent);
                    }
                    projectFileIsDirty = true;
                    projectItem.ProjectItems.AddFromFile(dependentConfigFullPath);
                    //project.ProjectItems.AddFromFile(dependentConfigFullPath);
                }
            }
        }
コード例 #5
0
        public void Given_Null_Path_When_Exists_Invoked_Then_It_Should_Return_False()
        {
            var wrapper = new FileWrapper();

            var result = wrapper.Exists(null);

            result.Should().BeFalse();
        }
コード例 #6
0
        public void DeleteCoverageReport(Guid resourceID, string reportName)
        {
            var dirPath      = GetTestCoveragePathForResourceId(resourceID);
            var testFilePath = Path.Combine(dirPath, $"{reportName}.coverage");

            if (_fileWrapper.Exists(testFilePath))
            {
                _fileWrapper.Delete(testFilePath);
                if (TestCoverageReports.TryGetValue(resourceID, out List <IServiceTestCoverageModelTo> coverageReports))
                {
                    var foundReportToDelete = coverageReports.FirstOrDefault(to => to.ReportName.Equals(reportName, StringComparison.InvariantCultureIgnoreCase));
                    if (foundReportToDelete != null)
                    {
                        Dev2Logger.Debug("Removing Report: " + reportName + Environment.NewLine + Environment.StackTrace, GlobalConstants.WarewolfDebug);
                        coverageReports.Remove(foundReportToDelete);
                    }
                }
            }
        }
コード例 #7
0
        public void Given_Path_When_Exists_Invoked_Then_It_Should_Return_Result(string filename, bool expected)
        {
            var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var path      = Path.Combine(directory, filename);

            var wrapper = new FileWrapper();

            var result = wrapper.Exists(path);

            result.Should().Be(expected);
        }
コード例 #8
0
        public void FileInfoWrapper_CreateDelete()
        {
            var fileName    = "c:\\FileInfoWrapper_Construct.txt";
            var fileWrapper = new FileWrapper();

            fileWrapper.WriteAllText(fileName, "FileInfoWrapper_Construct test test");

            IFileInfo fileInfo   = new FileInfoWrapper(new FileInfo(fileName));
            var       createTime = fileInfo.CreationTime;

            fileInfo.Delete();

            Assert.IsTrue(createTime > (DateTime.Now.Subtract(new TimeSpan(0, 0, 30))));

            Assert.IsFalse(fileWrapper.Exists(fileName));
        }
コード例 #9
0
        private ResourceCatalogResult DeleteImpl(Guid workspaceID, IEnumerable <IResource> resources, List <IResource> workspaceResources, bool deleteVersions = true)
        {
            IResource resource = resources.FirstOrDefault();

            if (workspaceID == Guid.Empty && deleteVersions)
            {
                if (resource != null)
                {
                    var explorerItems = _serverVersionRepository.GetVersions(resource.ResourceID);
                    explorerItems?.ForEach(a => _serverVersionRepository.DeleteVersion(resource.ResourceID, a.VersionInfo.VersionNumber, resource.GetResourcePath(workspaceID)));
                }
            }

            workspaceResources.Remove(resource);
            if (resource != null && _dev2FileWrapper.Exists(resource.FilePath))
            {
                _dev2FileWrapper.Delete(resource.FilePath);
            }
            if (resource != null)
            {
                var messages = new List <ICompileMessageTO>
                {
                    new CompileMessageTO
                    {
                        ErrorType      = ErrorType.Critical,
                        MessageID      = Guid.NewGuid(),
                        MessagePayload = "The resource has been deleted",
                        MessageType    = CompileMessageType.ResourceDeleted,
                        ServiceID      = resource.ResourceID
                    }
                };
                UpdateDependantResourceWithCompileMessages(workspaceID, resource, messages);
            }
            if (workspaceID == GlobalConstants.ServerWorkspaceID)
            {
                if (resource != null)
                {
                    ServiceActionRepo.Instance.RemoveFromCache(resource.ResourceID);
                    ServerAuthorizationService.Instance.Remove(resource.ResourceID);
                }
            }

            ((ResourceCatalog)_resourceCatalog).RemoveFromResourceActivityCache(workspaceID, resource);
            return(ResourceCatalogResultBuilder.CreateSuccessResult("Success"));
        }
コード例 #10
0
        public StringBuilder Execute(Dictionary <string, StringBuilder> values, IWorkspace theWorkspace)
        {
            var msg        = new ExecuteMessage();
            var serializer = new Dev2JsonSerializer();

            try
            {
                Dev2Logger.Info("Save Resource Service", GlobalConstants.WarewolfInfo);

                values.TryGetValue("ServerSettings", out StringBuilder resourceDefinition);

                var updatedServerSettings = serializer.Deserialize <ServerSettingsData>(resourceDefinition);

                var sourceFilePath = Config.Server.AuditFilePath;

                var auditsFilePath = updatedServerSettings.AuditFilePath;

                if (sourceFilePath != auditsFilePath)
                {
                    var   source = Path.Combine(sourceFilePath, "auditDB.db");
                    IFile _file  = new FileWrapper();
                    if (_file.Exists(source))
                    {
                        var destination = Path.Combine(auditsFilePath, "auditDB.db");
                        CreateIfNotExists(auditsFilePath);
                        _file.Move(source, destination);
                        Config.Server.AuditFilePath = auditsFilePath;
                        msg.Message = new StringBuilder("Moved");
                    }
                }
                else
                {
                    msg.Message = new StringBuilder();
                }
                msg.HasError = false;
            }
            catch (Exception err)
            {
                msg.HasError = true;
                msg.Message  = new StringBuilder(err.Message);
                Dev2Logger.Error(err, GlobalConstants.WarewolfError);
            }
            return(serializer.SerializeToBuilder(msg));
        }
コード例 #11
0
        public void DeleteTest(Guid resourceID, string testName)
        {
            var dirPath      = GetTestPathForResourceId(resourceID);
            var testFilePath = Path.Combine(dirPath, $"{testName}.test");

            if (_fileWrapper.Exists(testFilePath))
            {
                _fileWrapper.Delete(testFilePath);
                if (Tests.TryGetValue(resourceID, out List <IServiceTestModelTO> testList))
                {
                    var foundTestToDelete = testList.FirstOrDefault(to => to.TestName.Equals(testName, StringComparison.InvariantCultureIgnoreCase));
                    if (foundTestToDelete != null)
                    {
                        Dev2Logger.Debug("Removing Test: " + testName + Environment.NewLine + Environment.StackTrace, GlobalConstants.WarewolfDebug);
                        testList.Remove(foundTestToDelete);
                    }
                }
            }
        }
コード例 #12
0
        private static void CreateLikedConfigFilesNotFromProject(ProjectItem targetProjectItem)
        {
            var configName              = targetProjectItem.Name;
            var sourceConfigPath        = targetProjectItem.GetFullPath();
            var project                 = targetProjectItem.ContainingProject;
            var buildConfigurationNames = project.GetBuildConfigurationNames();

            var sourceConfigDirectory = Directory.GetParent(sourceConfigPath).FullName;

            foreach (var buildConfigurationName in buildConfigurationNames)
            {
                var dependentConfig = GetTransformConfigName(configName, buildConfigurationName);
                var sourceDependentConfigFullPath = Path.Combine(sourceConfigDirectory, dependentConfig);
                // check if source config file exist and not exist in target
                if (FileWrapper.Exists(sourceDependentConfigFullPath) &&
                    targetProjectItem.ProjectItems.AsEnumerable().All(c => c.Name != dependentConfig))
                {
                    targetProjectItem.ProjectItems.AddFromFile(sourceDependentConfigFullPath);
                }
            }
        }
コード例 #13
0
        // disclaimer: visual studio doesn't support adding dependent file under linked file
        // so no dependent transformed config under linked app.config in designer
        private static void CreateConfigFiles(Project project, ProjectItem projectItem)
        {
            var appConfigName           = projectItem.Name;
            var buildConfigurationNames = project.GetBuildConfigurationNames();
            // get app.config directory. new transform config will be created there.
            string path;

            if (projectItem.IsLink())
            {
                path = Directory.GetParent(projectItem.ContainingProject.FullName).FullName;
            }
            else
            {
                path = Directory.GetParent(projectItem.GetFullPath()).FullName;
            }

            foreach (var buildConfigurationName in buildConfigurationNames)
            {
                var dependentConfig         = GetTransformConfigName(appConfigName, buildConfigurationName);
                var dependentConfigFullPath = Path.Combine(path, dependentConfig);
                // check if config file exist
                if (FileWrapper.Exists(dependentConfigFullPath))
                {
                    VsService.OutputLine($"File {dependentConfig} already exists");
                }
                else
                {
                    using (var file = FileWrapper.AppendText(dependentConfigFullPath))
                    {
                        file.Write(DependencyConfigContent);
                    }
                    VsService.OutputLine($"File {dependentConfig} was added");
                }
                // add it to project file anyway, in case it was deleted just from project file
                projectItem.ProjectItems.AddFromFile(dependentConfigFullPath);
            }
        }
コード例 #14
0
        ResourceCatalogResult UpdateResourceName(Guid workspaceID, IResource resource, string newName, string resourcePath)
        {
            //rename where used
            RenameWhereUsed(_resourceCatalog.GetDependentsAsResourceForTrees(workspaceID, resource.ResourceID), workspaceID, resourcePath, newName);

            //rename resource
            var resourceContents = _resourceCatalog.GetResourceContents(workspaceID, resource.ResourceID);

            var resourceElement = resourceContents.ToXElement();
            //xml name attibute
            var    nameAttrib = resourceElement.Attribute("Name");
            string oldName    = null;

            if (nameAttrib == null)
            {
                resourceElement.Add(new XAttribute("Name", newName));
            }
            else
            {
                oldName = nameAttrib.Value;
                nameAttrib.SetValue(newName);
            }
            //xaml
            var actionElement = resourceElement.Element("Action");
            var xaml          = actionElement?.Element("XamlDefinition");

            xaml?.SetValue(xaml.Value
                           .Replace("x:Class=\"" + oldName, "x:Class=\"" + newName)
                           .Replace("ToolboxFriendlyName=\"" + oldName, "ToolboxFriendlyName=\"" + newName)
                           .Replace("DisplayName=\"" + oldName, "DisplayName=\"" + newName));
            //xml display name element
            var displayNameElement = resourceElement.Element("DisplayName");

            displayNameElement?.SetValue(newName);

            //delete old resource in local workspace without updating dependants with compile messages
            lock (Common.GetFileLock(resource.FilePath))
            {
                if (_dev2FileWrapper.Exists(resource.FilePath))
                {
                    lock (Common.GetFileLock(resource.FilePath))
                    {
                        _dev2FileWrapper.Delete(resource.FilePath);
                    }
                }
            }

            var resPath = resource.GetResourcePath(workspaceID);

            var savePath          = resPath;
            var resourceNameIndex = resPath.LastIndexOf(resource.ResourceName, StringComparison.InvariantCultureIgnoreCase);

            if (resourceNameIndex >= 0)
            {
                savePath = resPath.Substring(0, resourceNameIndex);
            }
            resource.ResourceName = newName;
            var contents = resourceElement.ToStringBuilder();

            return(((ResourceCatalog)_resourceCatalog).SaveImpl(workspaceID, resource, contents, savePath));
        }