public AddFile ( string filename ) : |
||
filename | string | /// Absolute path to the file. /// |
Résultat |
public async Task GetParentFileTest(string inputFile, string expectedParentFile) { string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln"); Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile); Project p = (Project)sol.Items [0]; var dir = p.BaseDirectory; string inputFileDestination = Path.Combine(dir, "FileNesting", inputFile); string parentFileDestination = Path.Combine(dir, "FileNesting", expectedParentFile); p.AddDirectory("FileNesting"); p.AddFile(inputFileDestination); p.AddFile(parentFileDestination); string parentFile = FileNestingService.GetParentFile(p, inputFileDestination); Assert.That(parentFile, Is.EqualTo(parentFileDestination), $"Was expecting parent file {parentFileDestination} for {inputFileDestination} but got {parentFile}"); // Now check we get nothing when parent file doesn't exist p.Files.Remove(parentFileDestination); parentFile = FileNestingService.GetParentFile(p, inputFileDestination); Assert.Null(parentFile, $"Was expecting no parent file for {inputFileDestination} but got {parentFile}"); sol.Dispose(); }
public override bool AddToProject (SolutionItem parent, Project project, string language, string directory, string name) { // Replace template variables string cname = Path.GetFileNameWithoutExtension (name); string[,] tags = { {"Name", cname}, }; string content = addinTemplate.OuterXml; content = StringParserService.Parse (content, tags); // Create the manifest XmlDocument doc = new XmlDocument (); doc.LoadXml (content); string file = Path.Combine (directory, "manifest.addin.xml"); doc.Save (file); project.AddFile (file, BuildAction.EmbeddedResource); AddinData.EnableAddinAuthoringSupport ((DotNetProject)project); return true; }
public override bool AddToProject (SolutionItem policyParent, Project project, string language, string directory, string name) { var path = FilePath.Build (directory, project.Name + "-res.zip"); var file = new ProjectFile (path, "EmbeddedResource"); file.ResourceId = "XobotOS.Resources"; file.Visible = false; project.AddFile (file); return true; }
public void MigrateFiles (Project source, Project target) { foreach (ProjectFile file in GetFilesToMigrate (source).ToArray ()) { FilePath destination = GetFileDestinationPath (file, source, target); Directory.CreateDirectory (destination.ParentDirectory); FileService.MoveFile (file.FilePath, destination); var movedProjectFile = new ProjectFile (destination, file.BuildAction); target.AddFile (movedProjectFile); source.Files.Remove (file); } }
public ProjectFile AddFileToProject (SolutionItem policyParent, Project project, string language, string directory, string name) { generatedFile = SaveFile (policyParent, project, language, directory, name); if (generatedFile != null) { string buildAction = this.buildAction ?? project.GetDefaultBuildAction (generatedFile); ProjectFile projectFile = project.AddFile (generatedFile, buildAction); if (!string.IsNullOrEmpty (dependsOn)) { Dictionary<string,string> tags = new Dictionary<string,string> (); ModifyTags (policyParent, project, language, name, generatedFile, ref tags); string parsedDepName = StringParserService.Parse (dependsOn, tags); if (projectFile.DependsOn != parsedDepName) projectFile.DependsOn = parsedDepName; } if (!string.IsNullOrEmpty (customTool)) projectFile.Generator = customTool; DotNetProject netProject = project as DotNetProject; if (netProject != null) { // Add required references foreach (string aref in references) { string res = netProject.AssemblyContext.GetAssemblyFullName (aref, netProject.TargetFramework); res = netProject.AssemblyContext.GetAssemblyNameForVersion (res, netProject.TargetFramework); if (!ContainsReference (netProject, res)) netProject.References.Add (new ProjectReference (ReferenceType.Package, aref)); } } return projectFile; } else return null; }
public void FileDependencies() { string solFile = Util.GetSampleProject("file-dependencies", "ConsoleProject.sln"); Solution sol = (Solution)Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile); Project p = (Project)sol.Items [0]; var dir = p.BaseDirectory; var file1 = p.Files.GetFile(dir.Combine("file1.xml")); var file2 = p.Files.GetFile(dir.Combine("file2.xml")); var file3 = p.Files.GetFile(dir.Combine("file3.xml")); var file4 = p.Files.GetFile(dir.Combine("file4.xml")); var file5 = p.Files.GetFile(dir.Combine("file5.xml")); Assert.AreEqual(file3, file1.DependsOnFile); Assert.AreEqual(file3, file2.DependsOnFile); Assert.AreEqual(2, file3.DependentChildren.Count); Assert.IsTrue(file3.DependentChildren.Contains(file1)); Assert.IsTrue(file3.DependentChildren.Contains(file2)); Assert.AreEqual(file5, file4.DependsOnFile); Assert.AreEqual(1, file5.DependentChildren.Count); Assert.IsTrue(file5.DependentChildren.Contains(file4)); // Change a dependency file1.DependsOn = ""; Assert.IsNull(file1.DependsOnFile); Assert.AreEqual(file3, file2.DependsOnFile); Assert.AreEqual(1, file3.DependentChildren.Count); Assert.IsTrue(file3.DependentChildren.Contains(file2)); // Unresolved dependency file1.DependsOn = "foo.xml"; Assert.IsNull(file1.DependsOnFile); var foo = p.AddFile(dir.Combine("foo.xml")); Assert.AreEqual(foo, file1.DependsOnFile); // Resolved dependency file2.DependsOn = "foo.xml"; Assert.AreEqual(foo, file2.DependsOnFile); Assert.AreEqual(0, file3.DependentChildren.Count); // Remove a file p.Files.Remove(file5); Assert.IsNull(file4.DependsOnFile); // Add a file file5 = p.AddFile(dir.Combine("file5.xml")); Assert.AreEqual(file5, file4.DependsOnFile); Assert.AreEqual(1, file5.DependentChildren.Count); Assert.IsTrue(file5.DependentChildren.Contains(file4)); }
static IType CreateClass (Project project, Stetic.ActionGroupComponent group, string name, string namspace, string folder) { string fullName = namspace.Length > 0 ? namspace + "." + name : name; CodeRefactorer gen = new CodeRefactorer (project.ParentSolution); CodeTypeDeclaration type = new CodeTypeDeclaration (); type.Name = name; type.IsClass = true; type.BaseTypes.Add (new CodeTypeReference ("Gtk.ActionGroup")); // Generate the constructor. It contains the call that builds the widget. CodeConstructor ctor = new CodeConstructor (); ctor.Attributes = MemberAttributes.Public | MemberAttributes.Final; ctor.BaseConstructorArgs.Add (new CodePrimitiveExpression (fullName)); CodeMethodInvokeExpression call = new CodeMethodInvokeExpression ( new CodeMethodReferenceExpression ( new CodeTypeReferenceExpression ("Stetic.Gui"), "Build" ), new CodeThisReferenceExpression (), new CodeTypeOfExpression (fullName) ); ctor.Statements.Add (call); type.Members.Add (ctor); // Add signal handlers foreach (Stetic.ActionComponent action in group.GetActions ()) { foreach (Stetic.Signal signal in action.GetSignals ()) { CodeMemberMethod met = new CodeMemberMethod (); met.Name = signal.Handler; met.Attributes = MemberAttributes.Family; met.ReturnType = new CodeTypeReference (signal.SignalDescriptor.HandlerReturnTypeName); foreach (Stetic.ParameterDescriptor pinfo in signal.SignalDescriptor.HandlerParameters) met.Parameters.Add (new CodeParameterDeclarationExpression (pinfo.TypeName, pinfo.Name)); type.Members.Add (met); } } // Create the class IType cls = null; cls = gen.CreateClass (project, ((DotNetProject)project).LanguageName, folder, namspace, type); if (cls == null) throw new UserException ("Could not create class " + fullName); project.AddFile (cls.CompilationUnit.FileName, BuildAction.Compile); IdeApp.ProjectOperations.Save (project); // Make sure the database is up-to-date ProjectDomService.Parse (project, cls.CompilationUnit.FileName); return cls; }
/// <summary> /// Adds files to a project, potentially asking the user whether to move, copy or link the files. /// </summary> public IList<ProjectFile> AddFilesToProject (Project project, FilePath[] files, FilePath targetDirectory, string buildAction) { int action = -1; IProgressMonitor monitor = null; if (files.Length > 10) { monitor = new MessageDialogProgressMonitor (true); monitor.BeginTask (GettextCatalog.GetString("Adding files..."), files.Length); } var newFileList = new List<ProjectFile> (); using (monitor) { foreach (FilePath file in files) { if (monitor != null) { monitor.Log.WriteLine (file); monitor.Step (1); } if (FileService.IsDirectory (file)) { //FIXME: warning about skipping? newFileList.Add (null); continue; } //files in the project directory get added directly in their current location without moving/copying if (file.IsChildPathOf (project.BaseDirectory)) { newFileList.Add (project.AddFile (file, buildAction)); continue; } //for files outside the project directory, we ask the user whether to move, copy or link var md = new Gtk.MessageDialog ( IdeApp.Workbench.RootWindow, Gtk.DialogFlags.Modal | Gtk.DialogFlags.DestroyWithParent, Gtk.MessageType.Question, Gtk.ButtonsType.None, GettextCatalog.GetString ("The file {0} is outside the project directory. What would you like to do?", file)); try { Gtk.CheckButton remember = null; if (files.Length > 1) { remember = new Gtk.CheckButton (GettextCatalog.GetString ("Use the same action for all selected files.")); md.VBox.PackStart (remember, false, false, 0); } const int ACTION_LINK = 3; const int ACTION_COPY = 1; const int ACTION_MOVE = 2; md.AddButton (GettextCatalog.GetString ("_Link"), ACTION_LINK); md.AddButton (Gtk.Stock.Copy, ACTION_COPY); md.AddButton (GettextCatalog.GetString ("_Move"), ACTION_MOVE); md.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel); md.VBox.ShowAll (); int ret = -1; if (action < 0) { ret = MessageService.RunCustomDialog (md); if (ret < 0) return newFileList; if (remember != null && remember.Active) action = ret; } else { ret = action; } var targetName = targetDirectory.Combine (file.FileName); if (ret == ACTION_LINK) { var pf = project.AddFile (file, buildAction); pf.Link = project.GetRelativeChildPath (targetName); newFileList.Add (pf); continue; } try { if (MoveCopyFile (file, targetName, ret == ACTION_MOVE)) newFileList.Add (project.AddFile (targetName, buildAction)); else newFileList.Add (null); } catch (Exception ex) { MessageService.ShowException (ex, GettextCatalog.GetString ( "An error occurred while attempt to move/copy that file. Please check your permissions.")); newFileList.Add (null); } } finally { md.Destroy (); } } } return newFileList; }
/// <summary> /// Adds files to a project, potentially asking the user whether to move, copy or link the files. /// </summary> public IList<ProjectFile> AddFilesToProject (Project project, FilePath[] files, FilePath[] targetPaths, string buildAction) { Debug.Assert (project != null); Debug.Assert (files != null); Debug.Assert (targetPaths != null); Debug.Assert (files.Length == targetPaths.Length); int action = -1; IProgressMonitor monitor = null; if (files.Length > 10) { monitor = new MessageDialogProgressMonitor (true); monitor.BeginTask (GettextCatalog.GetString("Adding files..."), files.Length); } var newFileList = new List<ProjectFile> (); //project.AddFile (string) does linear search for duplicate file, so instead we use this HashSet and //and add the ProjectFiles directly. With large project and many files, this should really help perf. //Also, this is a better check because we handle vpaths and links. //FIXME: it would be really nice if project.Files maintained these hashmaps var vpathsInProject = new HashSet<FilePath> (project.Files.Select (pf => pf.ProjectVirtualPath)); var filesInProject = new HashSet<FilePath> (project.Files.Select (pf => pf.FilePath)); using (monitor) { for (int i = 0; i < files.Length; i++) { FilePath file = files[i]; if (monitor != null) { monitor.Log.WriteLine (file); monitor.Step (1); } if (FileService.IsDirectory (file)) { //FIXME: warning about skipping? newFileList.Add (null); continue; } FilePath targetPath = targetPaths[i].CanonicalPath; Debug.Assert (targetPath.IsChildPathOf (project.BaseDirectory)); var vpath = targetPath.ToRelative (project.BaseDirectory); if (vpathsInProject.Contains (vpath)) { MessageService.ShowWarning (GettextCatalog.GetString ( "There is a already a file or link in the project with the name '{0}'", vpath)); continue; } string fileBuildAction = buildAction; if (string.IsNullOrEmpty (buildAction)) fileBuildAction = project.GetDefaultBuildAction (file); //files in the target directory get added directly in their current location without moving/copying if (file.CanonicalPath == targetPath) { //FIXME: MD project system doesn't cope with duplicate includes - project save/load will remove the file if (filesInProject.Contains (targetPath)) { var link = project.Files.GetFile (targetPath).Link; MessageService.ShowWarning (GettextCatalog.GetString ( "The link '{0}' in the project already includes the file '{1}'", link, file)); continue; } var pf = new ProjectFile (file, fileBuildAction); project.AddFile (pf); vpathsInProject.Add (pf.ProjectVirtualPath); filesInProject.Add (pf.FilePath); newFileList.Add (pf); continue; } //for files outside the project directory, we ask the user whether to move, copy or link var md = new Gtk.MessageDialog ( IdeApp.Workbench.RootWindow, Gtk.DialogFlags.Modal | Gtk.DialogFlags.DestroyWithParent, Gtk.MessageType.Question, Gtk.ButtonsType.None, GettextCatalog.GetString ("The file {0} is outside the target directory. What would you like to do?", file)); try { Gtk.CheckButton remember = null; if (files.Length > 1) { remember = new Gtk.CheckButton (GettextCatalog.GetString ("Use the same action for all selected files.")); md.VBox.PackStart (remember, false, false, 0); } const int ACTION_LINK = 3; const int ACTION_COPY = 1; const int ACTION_MOVE = 2; md.AddButton (GettextCatalog.GetString ("_Link"), ACTION_LINK); md.AddButton (Gtk.Stock.Copy, ACTION_COPY); md.AddButton (GettextCatalog.GetString ("_Move"), ACTION_MOVE); md.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel); md.VBox.ShowAll (); int ret = -1; if (action < 0) { ret = MessageService.RunCustomDialog (md); if (ret < 0) return newFileList; if (remember != null && remember.Active) action = ret; } else { ret = action; } if (ret == ACTION_LINK) { //FIXME: MD project system doesn't cope with duplicate includes - project save/load will remove the file if (filesInProject.Contains (file)) { var link = project.Files.GetFile (file).Link; MessageService.ShowWarning (GettextCatalog.GetString ( "The link '{0}' in the project already includes the file '{1}'", link, file)); continue; } var pf = new ProjectFile (file, fileBuildAction) { Link = vpath }; project.AddFile (pf); vpathsInProject.Add (pf.ProjectVirtualPath); filesInProject.Add (pf.FilePath); newFileList.Add (pf); continue; } try { if (!Directory.Exists (targetPath.ParentDirectory)) FileService.CreateDirectory (targetPath.ParentDirectory); if (MoveCopyFile (file, targetPath, ret == ACTION_MOVE)) { var pf = new ProjectFile (targetPath, fileBuildAction); project.AddFile (pf); vpathsInProject.Add (pf.ProjectVirtualPath); filesInProject.Add (pf.FilePath); newFileList.Add (pf); } else { newFileList.Add (null); } } catch (Exception ex) { MessageService.ShowException (ex, GettextCatalog.GetString ( "An error occurred while attempt to move/copy that file. Please check your permissions.")); newFileList.Add (null); } } finally { md.Destroy (); } } } return newFileList; }