Ejemplo n.º 1
0
        protected virtual async Task <bool> CreateFile(FileDescriptionTemplate newfile, SolutionFolderItem policyParent, Project project, SolutionFolder solutionFolder, string directory, string language, string name)
        {
            var tagModelProvider = (WorkspaceObject)project ?? (WorkspaceObject)solutionFolder;

            if (tagModelProvider != null)
            {
                var model = tagModelProvider.GetStringTagModel(new DefaultConfigurationSelector());
                newfile.SetProjectTagModel(model);
            }

            try {
                if (project != null)
                {
                    if (await newfile.AddToProjectAsync(policyParent, project, language, directory, name))
                    {
                        newfile.Show();
                        return(true);
                    }
                    return(false);
                }

                var singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                {
                    throw new InvalidOperationException("Single file template expected");
                }

                if (directory != null)
                {
                    string fileName = await singleFile.SaveFileAsync(policyParent, project, language, directory, name);

                    if (fileName != null)
                    {
                        if (solutionFolder != null)
                        {
                            if (solutionFolder.IsRoot)
                            {
                                // Don't allow adding files to the root folder. VS doesn't allow it
                                // If there is no existing folder, create one
                                solutionFolder = solutionFolder.ParentSolution.DefaultSolutionFolder;
                            }
                            solutionFolder.Files.Add(fileName);
                        }
                        IdeApp.Workbench.OpenDocument(fileName, project: null);
                        return(true);
                    }
                    return(false);
                }

                string unsavedFilename = singleFile.GetFileName(policyParent, project, language, directory, name);
                Stream stream          = singleFile.CreateFileContent(policyParent, project, language, unsavedFilename, name)
                                         ?? await singleFile.CreateFileContentAsync(policyParent, project, language, unsavedFilename, name);

                string mimeType = GuessMimeType(unsavedFilename);
                IdeApp.Workbench.NewDocument(unsavedFilename, mimeType, stream);
                return(true);
            } finally {
                newfile.SetProjectTagModel(null);
            }
        }
Ejemplo n.º 2
0
        protected virtual bool CreateFile(FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name)
        {
            if (project != null)
            {
                if (newfile.AddToProject(policyParent, project, language, directory, name))
                {
                    newfile.Show();
                    return(true);
                }
            }
            else
            {
                SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                {
                    throw new InvalidOperationException("Single file template expected");
                }

                if (directory != null)
                {
                    string fileName = singleFile.SaveFile(policyParent, project, language, directory, name);
                    if (fileName != null)
                    {
                        IdeApp.Workbench.OpenDocument(fileName);
                        return(true);
                    }
                }
                else
                {
                    string fileName = singleFile.GetFileName(policyParent, project, language, directory, name);
                    Stream stream   = singleFile.CreateFileContent(policyParent, project, language, fileName, name);

                    // Guess the mime type of the new file
                    string fn  = Path.GetTempFileName();
                    string ext = Path.GetExtension(fileName);
                    int    n   = 0;
                    while (File.Exists(fn + n + ext))
                    {
                        n++;
                    }
                    FileService.MoveFile(fn, fn + n + ext);
                    string mimeType = DesktopService.GetMimeTypeForUri(fn + n + ext);
                    FileService.DeleteFile(fn + n + ext);
                    if (mimeType == null || mimeType == "")
                    {
                        mimeType = "text";
                    }

                    IdeApp.Workbench.NewDocument(fileName, mimeType, stream);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        protected virtual async Task <bool> CreateFile(FileDescriptionTemplate newfile, SolutionFolderItem policyParent, Project project, string directory, string language, string name)
        {
            if (project != null)
            {
                var model = project.GetStringTagModel(new DefaultConfigurationSelector());
                newfile.SetProjectTagModel(model);
                try {
                    if (await newfile.AddToProjectAsync(policyParent, project, language, directory, name))
                    {
                        newfile.Show();
                        return(true);
                    }
                } finally {
                    newfile.SetProjectTagModel(null);
                }
            }
            else
            {
                var singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                {
                    throw new InvalidOperationException("Single file template expected");
                }

                if (directory != null)
                {
                    string fileName = await singleFile.SaveFileAsync(policyParent, project, language, directory, name);

                    if (fileName != null)
                    {
                        IdeApp.Workbench.OpenDocument(fileName, project);
                        return(true);
                    }
                }
                else
                {
                    string fileName = singleFile.GetFileName(policyParent, project, language, directory, name);
                    Stream stream   = singleFile.CreateFileContent(policyParent, project, language, fileName, name) ?? await singleFile.CreateFileContentAsync(policyParent, project, language, fileName, name);

                    string mimeType = GuessMimeType(fileName);
                    IdeApp.Workbench.NewDocument(fileName, mimeType, stream);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        protected virtual bool CreateFile(FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name)
        {
            if (project != null)
            {
                if (newfile.AddToProject(policyParent, project, language, directory, name))
                {
                    newfile.Show();
                    return(true);
                }
            }
            else
            {
                SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                {
                    throw new InvalidOperationException("Single file template expected");
                }

                if (directory != null)
                {
                    string fileName = singleFile.SaveFile(policyParent, project, language, directory, name);
                    if (fileName != null)
                    {
                        IdeApp.Workbench.OpenDocument(fileName);
                        return(true);
                    }
                }
                else
                {
                    string fileName = singleFile.GetFileName(policyParent, project, language, directory, name);
                    Stream stream   = singleFile.CreateFileContent(policyParent, project, language, fileName, name);

                    string mimeType = GuessMimeType(fileName);
                    IdeApp.Workbench.NewDocument(fileName, mimeType, stream);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
		protected virtual bool CreateFile (FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name)
        {
            if (project != null) {
				var model = project.GetStringTagModel (new DefaultConfigurationSelector ());
				newfile.SetProjectTagModel (model);
				try {
	                if (newfile.AddToProject (policyParent, project, language, directory, name)) {
	                    newfile.Show ();
	                    return true;
					}
				} finally {
					newfile.SetProjectTagModel (null);
				}
			} else {
                SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                    throw new InvalidOperationException ("Single file template expected");

                if (directory != null) {
                    string fileName = singleFile.SaveFile (policyParent, project, language, directory, name);
                    if (fileName != null) {
						IdeApp.Workbench.OpenDocument (fileName, project);
                        return true;
                    }
				} else {
                    string fileName = singleFile.GetFileName (policyParent, project, language, directory, name);
                    Stream stream = singleFile.CreateFileContent (policyParent, project, language, fileName, name);

					string mimeType = GuessMimeType (fileName);
					IdeApp.Workbench.NewDocument (fileName, mimeType, stream);
					return true;
                }
            }
            return false;
        }
Ejemplo n.º 6
0
		protected virtual bool CreateFile (FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name)
        {
            if (project != null) {
                if (newfile.AddToProject (policyParent, project, language, directory, name)) {
                    newfile.Show ();
                    return true;
                }
			} else {
                SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate;
                if (singleFile == null)
                    throw new InvalidOperationException ("Single file template expected");

                if (directory != null) {
                    string fileName = singleFile.SaveFile (policyParent, project, language, directory, name);
                    if (fileName != null) {
                        IdeApp.Workbench.OpenDocument (fileName);
                        return true;
                    }
				} else {
                    string fileName = singleFile.GetFileName (policyParent, project, language, directory, name);
                    Stream stream = singleFile.CreateFileContent (policyParent, project, language, fileName, name);

                    // Guess the mime type of the new file
                    string fn = Path.GetTempFileName ();
                    string ext = Path.GetExtension (fileName);
					int n=0;
                    while (File.Exists (fn + n + ext))
                        n++;
                    FileService.MoveFile (fn, fn + n + ext);
					string mimeType = DesktopService.GetMimeTypeForUri (fn + n + ext);
                    FileService.DeleteFile (fn + n + ext);
                    if (mimeType == null || mimeType == "")
                        mimeType = "text";

                    IdeApp.Workbench.NewDocument (fileName, mimeType, stream);
                    return true;
                }
            }
            return false;
        }