<commit_before>﻿// Copyright 2013 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the 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.

namespace WebSharper.VisualStudio

/// Defines VisualStudio templates for WebSharper.
module VSIntegration =
    open System
    open System.IO
    open System.Text.RegularExpressions
    module X = IntelliFactory.Core.XmlTools
    module NG = NuGet
    module VST = Templates
    module VX = Extensions
    type Content = Utils.Content

    let wsName, fsharpTools =
#if ZAFIR
        "Zafir", ["Zafir.FSharp"]
#else
        "WebSharper", []
#endif

    let getExtensionName () =
        "WebSharper"

    let getExtensionGuid () =
        Guid("371cf828-9e17-41cb-b014-496f3e9e7171")

    let getExtensionDecription () =
        "F#-to-JavaScript compiler and web application framework"

    let pattern = Regex(@"(\d+(\.\d+)*)(\-(\w+))?$")

    type VersionInfo =
        {
            FullVersion : string
            NumericVersion : Version
            PackageId : string
            VersionSuffix : option<string>
        }

        static member FromFileName(package: string) =
            let fn = Path.GetFileNameWithoutExtension(package)
            let m = pattern.Match(fn)
            if m.Success then
                Some {
                    FullVersion = m.Groups.[0].Value
                    NumericVersion = Version(m.Groups.[1].Value)
                    PackageId = fn.Substring(0, m.Groups.[1].Index - 1)
                    VersionSuffix =
                        match m.Groups.[4].Value with
                        | "" -> None
                        | v -> Some v
                }
            else None

    type Config =
        {
            NuPkgPath : string
            ExtraNuPkgPaths : Map<string, string>
            RootPath : string
            TemplatesPath : string
            VsixPath : string
        }

    let ( +/ ) a b =
        Path.Combine(a, b)

    type Common =
        {
            Config : Config
            Icon : VST.Icon
            VersionInfo : VersionInfo
        }

        static member Create(cfg) =
            let iconPath = cfg.RootPath +/ "tools" +/ "WebSharper.png"
            let icon = VST.Icon.FromFile(iconPath)
            {
                Config = cfg
                Icon = icon
                VersionInfo = VersionInfo.FromFileName(cfg.NuPkgPath).Value
            }

    let getIdentity () =
        VST.ExtensionIdentity.Create(getExtensionName (), getExtensionGuid ())

    type TemplateDef =
        {
            PathName : string
            Name : string
            DefaultProjectName : string
            Description : string
            ProjectFile : string
            Files :
                (string -> Templates.FolderElement)
                -> (string -> Templates.FolderElement list -> Templates.FolderElement)
                -> Templates.FolderElement list
            ExtraNuGetPackages : string list
        }

    let makeTemplateMetadata com name dpn desc =
        VST.TemplateData.Create(VST.ProjectType.FSharp,
            name = name,
            description = desc,
            icon = com.Icon)
            .WithDefaultProjectName(dpn)

    let readNugetPackage path =
        let c = Content.ReadBinaryFile(path)
        let vn = VersionInfo.FromFileName(path).Value
        NG.Package.Create(vn.PackageId, vn.FullVersion, c)

    let makeProjectTemplate com def =
        let dir = com.Config.TemplatesPath +/ def.PathName
        let file name =
            VST.ProjectItem.FromTextFile(dir +/ name).ReplaceParameters()
            |> VST.FolderElement.Nested
        let folder name xs =
            VST.Folder.Create(name, xs)
            |> VST.FolderElement.Folder
        let meta = makeTemplateMetadata com def.Name def.DefaultProjectName def.Description
        let project =
            VST.Project.FromFile(dir +/ def.ProjectFile, def.Files file folder)
                .ReplaceParameters()
        let identity = getIdentity ()
        let extraPkgs =
            def.ExtraNuGetPackages
            |> List.map (fun x -> readNugetPackage (Map.find x com.Config.ExtraNuPkgPaths))
        let pkgs = readNugetPackage com.Config.NuPkgPath :: extraPkgs
        let nuGet = VST.NuGetPackages.Create(identity, pkgs)
        VST.ProjectTemplate.Create(meta, project)
            .WithNuGetPackages(nuGet)

    let libraryTemplate =
        {
            Name = "Library"
            PathName = "library"
            DefaultProjectName = "Library"
            Description =
                "Creates an F# library capable of containing WebSharper-compiled code."
            ProjectFile = "Library.fsproj"
            Files = fun file folder -> [file "Main.fs"]
            ExtraNuGetPackages = fsharpTools 
        }

    let extensionTemplate =
        {
            Name = "Extension"
            PathName = "extension"
            DefaultProjectName = "Extension"
            Description =<commit_msg>Report not avalilable NuGet package for templates<commit_after>﻿// Copyright 2013 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the 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.

namespace WebSharper.VisualStudio

/// Defines VisualStudio templates for WebSharper.
module VSIntegration =
    open System
    open System.IO
    open System.Text.RegularExpressions
    module X = IntelliFactory.Core.XmlTools
    module NG = NuGet
    module VST = Templates
    module VX = Extensions
    type Content = Utils.Content

    let wsName, fsharpTools =
#if ZAFIR
        "Zafir", ["Zafir.FSharp"]
#else
        "WebSharper", []
#endif

    let getExtensionName () =
        "WebSharper"

    let getExtensionGuid () =
        Guid("371cf828-9e17-41cb-b014-496f3e9e7171")

    let getExtensionDecription () =
        "F#-to-JavaScript compiler and web application framework"

    let pattern = Regex(@"(\d+(\.\d+)*)(\-(\w+))?$")

    type VersionInfo =
        {
            FullVersion : string
            NumericVersion : Version
            PackageId : string
            VersionSuffix : option<string>
        }

        static member FromFileName(package: string) =
            let fn = Path.GetFileNameWithoutExtension(package)
            let m = pattern.Match(fn)
            if m.Success then
                Some {
                    FullVersion = m.Groups.[0].Value
                    NumericVersion = Version(m.Groups.[1].Value)
                    PackageId = fn.Substring(0, m.Groups.[1].Index - 1)
                    VersionSuffix =
                        match m.Groups.[4].Value with
                        | "" -> None
                        | v -> Some v
                }
            else None

    type Config =
        {
            NuPkgPath : string
            ExtraNuPkgPaths : Map<string, string>
            RootPath : string
            TemplatesPath : string
            VsixPath : string
        }

    let ( +/ ) a b =
        Path.Combine(a, b)

    type Common =
        {
            Config : Config
            Icon : VST.Icon
            VersionInfo : VersionInfo
        }

        static member Create(cfg) =
            let iconPath = cfg.RootPath +/ "tools" +/ "WebSharper.png"
            let icon = VST.Icon.FromFile(iconPath)
            {
                Config = cfg
                Icon = icon
                VersionInfo = VersionInfo.FromFileName(cfg.NuPkgPath).Value
            }

    let getIdentity () =
        VST.ExtensionIdentity.Create(getExtensionName (), getExtensionGuid ())

    type TemplateDef =
        {
            PathName : string
            Name : string
            DefaultProjectName : string
            Description : string
            ProjectFile : string
            Files :
                (string -> Templates.FolderElement)
                -> (string -> Templates.FolderElement list -> Templates.FolderElement)
                -> Templates.FolderElement list
            ExtraNuGetPackages : string list
        }

    let makeTemplateMetadata com name dpn desc =
        VST.TemplateData.Create(VST.ProjectType.FSharp,
            name = name,
            description = desc,
            icon = com.Icon)
            .WithDefaultProjectName(dpn)

    let readNugetPackage path =
        let c = Content.ReadBinaryFile(path)
        let vn = VersionInfo.FromFileName(path).Value
        NG.Package.Create(vn.PackageId, vn.FullVersion, c)

    let makeProjectTemplate com def =
        let dir = com.Config.TemplatesPath +/ def.PathName
        let file name =
            VST.ProjectItem.FromTextFile(dir +/ name).ReplaceParameters()
            |> VST.FolderElement.Nested
        let folder name xs =
            VST.Folder.Create(name, xs)
            |> VST.FolderElement.Folder
        let meta = makeTemplateMetadata com def.Name def.DefaultProjectName def.Description
        let project =
            VST.Project.FromFile(dir +/ def.ProjectFile, def.Files file folder)
                .ReplaceParameters()
        let identity = getIdentity ()
        let findNugetPackage x =
            match Map.tryFind x com.Config.ExtraNuPkgPaths with
            | Some p -> readNugetPackage p
            | _ -> failwithf "Cannot find NuGet package for template: %s" x
        let extraPkgs = def.ExtraNuGetPackages |> List.map findNugetPackage
        let pkgs = readNugetPackage com.Config.NuPkgPath :: extraPkgs
        let nuGet = VST.NuGetPackages.Create(identity, pkgs)
        VST.ProjectTemplate.Create(meta, project)
            .WithNuGetPackages(nuGet)

    let libraryTemplate =
        {
            Name = "Library"
            PathName = "library"
            DefaultProjectName = "Library"
            Description =
                "Creates an F# library capable of containing WebSharper-compiled code."
            ProjectFile = "Library.fsproj"
            Files = fun file folder -> [file "Main.fs"]
            ExtraNuGetPackages = fsharpTools 
        }

    let extensionTemplate =
        {
            Name = "Extension"
            PathName = "extension"
            DefaultProjectName = "Extension"
            Description =
{'fraction_non_alphanumeric': 0.0839749977353021, 'fraction_numerical': 0.006703505752332639, 'mean_word_length': 1.7524308152580403, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220498', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2852, 'n_words': 775}