Beispiel #1
0
        internal override ProjectNode CreateProject()
        {
            NodejsProjectNode project = new NodejsProjectNode((NodejsProjectPackage)Package);

            project.SetSite((IOleServiceProvider)((IServiceProvider)Package).GetService(typeof(IOleServiceProvider)));
            return(project);
        }
Beispiel #2
0
        internal static void CreateNewFile(NodejsProjectNode projectNode, uint containerId)
        {
            using (var dialog = new NewFileNameForm(""))
            {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var itemName = dialog.TextBox.Text;
                    if (string.IsNullOrWhiteSpace(itemName))
                    {
                        return;
                    }
                    itemName = itemName.Trim();

                    var pResult = new VSADDRESULT[1];
                    projectNode.AddItem(
                        containerId,                                 // Identifier of the container folder.
                        VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE,    // Indicate that we want to create this new file by cloning a template file.
                        itemName,
                        1,                                           // Number of templates in the next parameter. Must be 1 if using VSADDITEMOP_CLONEFILE.
                        new string[] { Path.GetTempFileName() },     // Array contains the template file path.
                        IntPtr.Zero,                                 // Handle to the Add Item dialog box. Must be Zero if using VSADDITEMOP_CLONEFILE.
                        pResult);
                }
            }
        }
Beispiel #3
0
        public void GetLongSubPaths()
        {
            string
                basePath       = CreateTempPathOfLength(248 - 1 - @"\d\d".Length),
                shortFile      = basePath + @"\f",
                shortDir       = basePath + @"\d",
                shortShortFile = shortDir + @"\f",
                shortShortDir  = shortDir + @"\d",
                shortLongDir   = shortDir + @"\dd",
                shortLongFile  = shortDir + @"\f.ffffffffffffff",
                longFile       = basePath + @"\f.ffffffffffffff",
                longDir        = basePath + @"\d.dd",
                longLongFile   = longDir + @"\ff",
                longLongDir    = longDir + @"\dd";

            try
            {
                foreach (var path in new[] { shortDir, shortShortDir, shortLongDir, longDir, longLongDir })
                {
                    Console.WriteLine("CreateDirectory {0}", path);
                    Assert.IsTrue(NativeMethods.CreateDirectory(@"\\?\" + path, IntPtr.Zero), path);
                }

                File.WriteAllText(shortFile, "");
                foreach (var path in new[] { shortShortFile, shortLongFile, longFile, longLongFile })
                {
                    Console.WriteLine("CopyFile {0}", path);
                    Assert.IsTrue(NativeMethods.CopyFile(shortFile, @"\\?\" + path, true), path);
                }

                var longPaths = NodejsProjectNode.GetLongSubPaths(basePath).ToList();

                // Single() acts as assert here (throws if it doesn't find the element matching the condition).
                longPaths.Remove(longPaths.Single(lpi => lpi.FullPath == shortLongFile && !lpi.IsDirectory));
                longPaths.Remove(longPaths.Single(lpi => lpi.FullPath == shortLongDir && lpi.IsDirectory));
                longPaths.Remove(longPaths.Single(lpi => lpi.FullPath == longFile && !lpi.IsDirectory));
                longPaths.Remove(longPaths.Single(lpi => lpi.FullPath == longDir && lpi.IsDirectory));
                // longLongFile and longLongDir should not be reported, because their parent longDir is already reported.

                // There should be no other elements reported.
                Assert.AreEqual(0, longPaths.Count);
            }
            finally
            {
                DeleteLongPath(basePath);
            }
        }
Beispiel #4
0
        private static void CreateNewFile(NodejsProjectNode projectNode, uint containerId, string fileType)
        {
            using (var dialog = new NewFileNameForm(GetInitialName(fileType))) {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string itemName = dialog.TextBox.Text;

                    VSADDRESULT[] pResult = new VSADDRESULT[1];
                    projectNode.AddItem(
                        containerId,                                 // Identifier of the container folder.
                        VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE,    // Indicate that we want to create this new file by cloning a template file.
                        itemName,
                        1,                                           // Number of templates in the next parameter. Must be 1 if using VSADDITEMOP_CLONEFILE.
                        new string[] { Path.GetTempFileName() },     // Array contains the template file path.
                        IntPtr.Zero,                                 // Handle to the Add Item dialog box. Must be Zero if using VSADDITEMOP_CLONEFILE.
                        pResult);
                }
            }
        }
        private static void CreateNewFile(NodejsProjectNode projectNode, uint containerId, string fileType) {
            using (var dialog = new NewFileNameForm(GetInitialName(fileType))) {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                    string itemName = dialog.TextBox.Text;

                    VSADDRESULT[] pResult = new VSADDRESULT[1];
                    projectNode.AddItem(
                        containerId,                                 // Identifier of the container folder. 
                        VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE,    // Indicate that we want to create this new file by cloning a template file.
                        itemName,
                        1,                                           // Number of templates in the next parameter. Must be 1 if using VSADDITEMOP_CLONEFILE.
                        new string[] { GetTemplateFile(fileType) },  // Array contains the template file path.
                        IntPtr.Zero,                                 // Handle to the Add Item dialog box. Must be Zero if using VSADDITEMOP_CLONEFILE.
                        pResult
                    );

                    // TODO: Do we need to check if result[0] = VSADDRESULT.ADDRESULT_Success here?
                }
            }
        }
        internal static void CreateNewFile(NodejsProjectNode projectNode, uint containerId) {
            using (var dialog = new NewFileNameForm("")) {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                    string itemName = dialog.TextBox.Text;
                    if (string.IsNullOrWhiteSpace(itemName)) {
                        return;
                    }
                    itemName = itemName.Trim();

                    VSADDRESULT[] pResult = new VSADDRESULT[1];
                    projectNode.AddItem(
                        containerId,                                 // Identifier of the container folder. 
                        VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE,    // Indicate that we want to create this new file by cloning a template file.
                        itemName,
                        1,                                           // Number of templates in the next parameter. Must be 1 if using VSADDITEMOP_CLONEFILE.
                        new string[] { Path.GetTempFileName() },     // Array contains the template file path.
                        IntPtr.Zero,                                 // Handle to the Add Item dialog box. Must be Zero if using VSADDITEMOP_CLONEFILE.
                        pResult);
                }
            }
        }
Beispiel #7
0
 internal static void NavigateTo(string filename, int pos)
 {
     VsUtilities.NavigateTo(Instance, filename, NodejsProjectNode.IsNodejsFile(filename) ? typeof(NodejsEditorFactory).GUID : Guid.Empty, pos);
 }
Beispiel #8
0
 internal static void CreateNewCSSFile(NodejsProjectNode projectNode, uint containerId)
 {
     CreateNewFile(projectNode, containerId, NodejsConstants.CSS);
 }
Beispiel #9
0
 internal static void CreateNewTypeScriptFile(NodejsProjectNode projectNode, uint containerId)
 {
     CreateNewFile(projectNode, containerId, NodejsConstants.TypeScript);
 }
Beispiel #10
0
        public async Task <ExecutionResult> Execute(IReplWindow window, string arguments)
        {
            string projectPath  = string.Empty;
            string npmArguments = arguments.Trim(' ', '\t');

            // Parse project name/directory in square brackets
            if (npmArguments.StartsWith("[", StringComparison.Ordinal))
            {
                var match = Regex.Match(npmArguments, @"(?:[[]\s*\""?\s*)(.*?)(?:\s*\""?\s*[]]\s*)");
                projectPath  = match.Groups[1].Value;
                npmArguments = npmArguments.Substring(match.Length);
            }

            // Include spaces on either side of npm arguments so that we can more simply detect arguments
            // at beginning and end of string (e.g. '--global')
            npmArguments = string.Format(CultureInfo.InvariantCulture, " {0} ", npmArguments);

            // Prevent running `npm init` without the `-y` flag since it will freeze the repl window,
            // waiting for user input that will never come.
            if (npmArguments.Contains(" init ") && !(npmArguments.Contains(" -y ") || npmArguments.Contains(" --yes ")))
            {
                window.WriteError(SR.GetString(SR.ReplWindowNpmInitNoYesFlagWarning));
                return(ExecutionResult.Failure);
            }

            var solution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution;
            IEnumerable <IVsProject> loadedProjects = solution.EnumerateLoadedProjects(onlyNodeProjects: false);

            var projectNameToDirectoryDictionary = new Dictionary <string, Tuple <string, IVsHierarchy> >(StringComparer.OrdinalIgnoreCase);

            foreach (IVsProject project in loadedProjects)
            {
                var    hierarchy = (IVsHierarchy)project;
                object extObject;

                var projectResult = hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out extObject);
                if (!ErrorHandler.Succeeded(projectResult))
                {
                    continue;
                }

                EnvDTE.Project dteProject = extObject as EnvDTE.Project;
                if (dteProject == null)
                {
                    continue;
                }

                string projectName = dteProject.Name;
                if (string.IsNullOrEmpty(projectName))
                {
                    continue;
                }

                // Try checking the `ProjectHome` property first
                EnvDTE.Properties properties = dteProject.Properties;
                if (dteProject.Properties != null)
                {
                    EnvDTE.Property projectHome = null;
                    try {
                        projectHome = properties.Item("ProjectHome");
                    } catch (ArgumentException) {
                        // noop
                    }

                    if (projectHome != null)
                    {
                        var projectHomeDirectory = projectHome.Value as string;
                        if (!string.IsNullOrEmpty(projectHomeDirectory))
                        {
                            projectNameToDirectoryDictionary.Add(projectName, Tuple.Create(projectHomeDirectory, hierarchy));
                            continue;
                        }
                    }
                }

                // Otherwise, fall back to using fullname
                string projectDirectory = string.IsNullOrEmpty(dteProject.FullName) ? null : Path.GetDirectoryName(dteProject.FullName);
                if (!string.IsNullOrEmpty(projectDirectory))
                {
                    projectNameToDirectoryDictionary.Add(projectName, Tuple.Create(projectDirectory, hierarchy));
                }
            }

            Tuple <string, IVsHierarchy> projectInfo;

            if (string.IsNullOrEmpty(projectPath) && projectNameToDirectoryDictionary.Count == 1)
            {
                projectInfo = projectNameToDirectoryDictionary.Values.First();
            }
            else
            {
                projectNameToDirectoryDictionary.TryGetValue(projectPath, out projectInfo);
            }

            NodejsProjectNode nodejsProject = null;

            if (projectInfo != null)
            {
                projectPath = projectInfo.Item1;
                if (projectInfo.Item2 != null)
                {
                    nodejsProject = projectInfo.Item2.GetProject().GetNodejsProject();
                }
            }

            bool isGlobalCommand = false;

            if (string.IsNullOrWhiteSpace(npmArguments) ||
                npmArguments.Contains(" -g ") || npmArguments.Contains(" --global "))
            {
                projectPath     = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                isGlobalCommand = true;
            }

            // In case someone copies filename
            string projectDirectoryPath = File.Exists(projectPath) ? Path.GetDirectoryName(projectPath) : projectPath;

            if (!isGlobalCommand && !Directory.Exists(projectDirectoryPath))
            {
                window.WriteError("Please specify a valid Node.js project or project directory. If your solution contains multiple projects, specify a target project using .npm [ProjectName or ProjectDir] <npm arguments> For example: .npm [MyApp] list");
                return(ExecutionResult.Failure);
            }

            string npmPath;

            try {
                npmPath = NpmHelpers.GetPathToNpm(
                    nodejsProject != null ?
                    Nodejs.GetAbsoluteNodeExePath(
                        nodejsProject.ProjectHome,
                        nodejsProject.GetProjectProperty(NodeProjectProperty.NodeExePath))
                        : null);
            } catch (NpmNotFoundException) {
                Nodejs.ShowNodejsNotInstalled();
                return(ExecutionResult.Failure);
            }

            var npmReplRedirector = new NpmReplRedirector(window);

            await ExecuteNpmCommandAsync(
                npmReplRedirector,
                npmPath,
                projectDirectoryPath,
                new[] { npmArguments },
                null);

            if (npmReplRedirector.HasErrors)
            {
                window.WriteError(SR.GetString(SR.NpmReplCommandCompletedWithErrors, arguments));
            }
            else
            {
                window.WriteLine(SR.GetString(SR.NpmSuccessfullyCompleted, arguments));
            }

            if (nodejsProject != null)
            {
                await nodejsProject.CheckForLongPaths(npmArguments);
            }

            return(ExecutionResult.Success);
        }
        internal override ProjectNode CreateProject()
        {
            NodejsProjectNode project = new NodejsProjectNode((NodejsProjectPackage)Site);

            return(project);
        }
 internal static void CreateNewCSSFile(NodejsProjectNode projectNode, uint containerId) {
     CreateNewFile(projectNode, containerId, NodejsConstants.CSS);
 }
 internal static void CreateNewTypeScriptFile(NodejsProjectNode projectNode, uint containerId) {
     CreateNewFile(projectNode, containerId, NodejsConstants.TypeScript);
 }