From 3d90b8e74851ae0180bb4acaff5ea883f6983b7c Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 24 Feb 2016 18:13:27 -0800 Subject: [PATCH 1/4] Lowercase and trim npm package names for templates Adds a little extra normalization when converting a VS project names to npm project names in the templates: * Lowercase names. Inserts hypens between camelcased strings: "ExpressApp4" -> "express-app-4" * Trim to name to 214 characters closes #705 --- .../NodejsPackageParametersExtension.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs index a01f629ee..0ced0fbf1 100644 --- a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs +++ b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs @@ -14,23 +14,36 @@ // //*********************************************************// +using System; using System.Collections.Generic; using System.Text.RegularExpressions; using EnvDTE; using Microsoft.VisualStudio.TemplateWizard; - + namespace Microsoft.NodejsTools.ProjectWizard { class NodejsPackageParametersExtension : IWizard { - public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { - var projectName = replacementsDictionary["$projectname$"]; + private const int NpmPackageNameMaxLength = 214; - // Remove all leading url-invalid, underscore, and period characters from the string + /// + /// Normalize a project name to be a valid Npm package name. + /// + /// Name of a VS project. + private string GetNpmSafeProjectName(string projectName) { + // Remove all leading url-invalid, underscore, and period characters var npmProjectNameTransform = Regex.Replace(projectName, "^[^a-zA-Z0-9-~]*", string.Empty); // Replace all invalid characters with a dash - npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "[^a-zA-Z0-9-_~.]", "-"); + npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "[^a-zA-Z0-9-_~.]", "-"); + + // insert hypens between camelcased sections. + npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "([a-z0-9])([A-Z])", "$1-$2").ToLowerInvariant(); + + return npmProjectNameTransform.Substring(0, Math.Min(npmProjectNameTransform.Length, NpmPackageNameMaxLength)); + } - replacementsDictionary.Add("$npmsafeprojectname$", npmProjectNameTransform); + public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { + var projectName = replacementsDictionary["$projectname$"]; + replacementsDictionary.Add("$npmsafeprojectname$", GetNpmSafeProjectName(projectName)); } public void ProjectFinishedGenerating(EnvDTE.Project project) { From 81ba3db40e92d669e790b6b55101b710d65c104c Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 25 Feb 2016 10:40:47 -0800 Subject: [PATCH 2/4] Mark function as static --- .../Product/ProjectWizard/NodejsPackageParametersExtension.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs index 0ced0fbf1..2004fc5b3 100644 --- a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs +++ b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs @@ -28,7 +28,7 @@ class NodejsPackageParametersExtension : IWizard { /// Normalize a project name to be a valid Npm package name. /// /// Name of a VS project. - private string GetNpmSafeProjectName(string projectName) { + private static string NormalizeNpmPackageName(string projectName) { // Remove all leading url-invalid, underscore, and period characters var npmProjectNameTransform = Regex.Replace(projectName, "^[^a-zA-Z0-9-~]*", string.Empty); @@ -43,7 +43,7 @@ private string GetNpmSafeProjectName(string projectName) { public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { var projectName = replacementsDictionary["$projectname$"]; - replacementsDictionary.Add("$npmsafeprojectname$", GetNpmSafeProjectName(projectName)); + replacementsDictionary.Add("$npmsafeprojectname$", NormalizeNpmPackageName(projectName)); } public void ProjectFinishedGenerating(EnvDTE.Project project) { From a7ff928dfeb95f72411910c5f4e1a02fa7edaa9a Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 25 Feb 2016 13:27:21 -0800 Subject: [PATCH 3/4] Fix spelling and reorder interface Small fix to spelling and reordering to put private methods last based on review comments. --- .../NodejsPackageParametersExtension.cs | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs index 2004fc5b3..aa3d7e4a9 100644 --- a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs +++ b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs @@ -1,69 +1,69 @@ -//*********************************************************// -// Copyright (c) Microsoft. All rights reserved. -// -// Apache 2.0 License -// -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -// implied. See the License for the specific language governing -// permissions and limitations under the License. -// -//*********************************************************// - -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; -using EnvDTE; -using Microsoft.VisualStudio.TemplateWizard; +//*********************************************************// +// Copyright (c) Microsoft. All rights reserved. +// +// Apache 2.0 License +// +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. See the License for the specific language governing +// permissions and limitations under the License. +// +//*********************************************************// + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using EnvDTE; +using Microsoft.VisualStudio.TemplateWizard; + +namespace Microsoft.NodejsTools.ProjectWizard { + class NodejsPackageParametersExtension : IWizard { + public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { + var projectName = replacementsDictionary["$projectname$"]; + replacementsDictionary.Add("$npmsafeprojectname$", NormalizeNpmPackageName(projectName)); + } + + public void ProjectFinishedGenerating(EnvDTE.Project project) { + return; + } + + public void ProjectItemFinishedGenerating(ProjectItem projectItem) { + return; + } + + public bool ShouldAddProjectItem(string filePath) { + return true; + } + + public void BeforeOpeningFile(ProjectItem projectItem) { + return; + } + + public void RunFinished() { + return; + } + + private const int NpmPackageNameMaxLength = 214; -namespace Microsoft.NodejsTools.ProjectWizard { - class NodejsPackageParametersExtension : IWizard { - private const int NpmPackageNameMaxLength = 214; - /// - /// Normalize a project name to be a valid Npm package name. + /// Normalize a project name to be a valid Npm package name: https://docs.npmjs.com/files/package.json#name /// /// Name of a VS project. private static string NormalizeNpmPackageName(string projectName) { // Remove all leading url-invalid, underscore, and period characters - var npmProjectNameTransform = Regex.Replace(projectName, "^[^a-zA-Z0-9-~]*", string.Empty); - - // Replace all invalid characters with a dash + var npmProjectNameTransform = Regex.Replace(projectName, "^[^a-zA-Z0-9-~]*", string.Empty); + + // Replace all invalid characters with a dash npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "[^a-zA-Z0-9-_~.]", "-"); - // insert hypens between camelcased sections. + // Insert hyphens between camelcased sections. npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "([a-z0-9])([A-Z])", "$1-$2").ToLowerInvariant(); return npmProjectNameTransform.Substring(0, Math.Min(npmProjectNameTransform.Length, NpmPackageNameMaxLength)); - } - - public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { - var projectName = replacementsDictionary["$projectname$"]; - replacementsDictionary.Add("$npmsafeprojectname$", NormalizeNpmPackageName(projectName)); - } - - public void ProjectFinishedGenerating(EnvDTE.Project project) { - return; - } - - public void ProjectItemFinishedGenerating(ProjectItem projectItem) { - return; - } - - public bool ShouldAddProjectItem(string filePath) { - return true; - } - - public void BeforeOpeningFile(ProjectItem projectItem) { - return; - } - - public void RunFinished() { - return; - } - } -} + } + } +} From 6135358190847e757cd7a87c35a0a4eb33868fc5 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 25 Feb 2016 13:30:30 -0800 Subject: [PATCH 4/4] Fix for line endings getting messed up in previous commit --- .../NodejsPackageParametersExtension.cs | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs index aa3d7e4a9..406411bbf 100644 --- a/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs +++ b/Nodejs/Product/ProjectWizard/NodejsPackageParametersExtension.cs @@ -1,50 +1,50 @@ -//*********************************************************// -// Copyright (c) Microsoft. All rights reserved. -// -// Apache 2.0 License -// -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -// implied. See the License for the specific language governing -// permissions and limitations under the License. -// -//*********************************************************// +//*********************************************************// +// Copyright (c) Microsoft. All rights reserved. +// +// Apache 2.0 License +// +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. See the License for the specific language governing +// permissions and limitations under the License. +// +//*********************************************************// + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using EnvDTE; +using Microsoft.VisualStudio.TemplateWizard; -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; -using EnvDTE; -using Microsoft.VisualStudio.TemplateWizard; - -namespace Microsoft.NodejsTools.ProjectWizard { - class NodejsPackageParametersExtension : IWizard { - public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { - var projectName = replacementsDictionary["$projectname$"]; - replacementsDictionary.Add("$npmsafeprojectname$", NormalizeNpmPackageName(projectName)); - } - - public void ProjectFinishedGenerating(EnvDTE.Project project) { - return; - } - - public void ProjectItemFinishedGenerating(ProjectItem projectItem) { - return; - } - - public bool ShouldAddProjectItem(string filePath) { - return true; - } - - public void BeforeOpeningFile(ProjectItem projectItem) { - return; - } - - public void RunFinished() { - return; +namespace Microsoft.NodejsTools.ProjectWizard { + class NodejsPackageParametersExtension : IWizard { + public void RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) { + var projectName = replacementsDictionary["$projectname$"]; + replacementsDictionary.Add("$npmsafeprojectname$", NormalizeNpmPackageName(projectName)); + } + + public void ProjectFinishedGenerating(EnvDTE.Project project) { + return; + } + + public void ProjectItemFinishedGenerating(ProjectItem projectItem) { + return; + } + + public bool ShouldAddProjectItem(string filePath) { + return true; + } + + public void BeforeOpeningFile(ProjectItem projectItem) { + return; + } + + public void RunFinished() { + return; } private const int NpmPackageNameMaxLength = 214; @@ -64,6 +64,6 @@ private static string NormalizeNpmPackageName(string projectName) { npmProjectNameTransform = Regex.Replace(npmProjectNameTransform, "([a-z0-9])([A-Z])", "$1-$2").ToLowerInvariant(); return npmProjectNameTransform.Substring(0, Math.Min(npmProjectNameTransform.Length, NpmPackageNameMaxLength)); - } - } -} + } + } +}