<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using OfficeDevPnP.Core.Utilities;
using SharePointPnP.PowerShell.Commands.Enums;
using File = System.IO.File;
using Resources = SharePointPnP.PowerShell.Commands.Properties.Resources;


namespace SharePointPnP.PowerShell.Commands.Branding
{
    [Cmdlet(VerbsCommon.Get, "SPOProvisioningTemplate", SupportsShouldProcess = true)]
    [CmdletHelp("Generates a provisioning template from a web",
        Category = CmdletHelpCategory.Branding)]
    [CmdletExample(
       Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp",
       Remarks = "Extracts a provisioning template in Office Open XML from the current web.",
       SortOrder = 1)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.xml",
       Remarks = "Extracts a provisioning template in XML format from the current web.",
       SortOrder = 2)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -Schema V201503",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and saves it in the V201503 version of the schema.",
        SortOrder = 3)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -IncludeAllTermGroups",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and includes all term groups, term sets and terms from the Managed Metadata Service Taxonomy.",
        SortOrder = 4)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -IncludeSiteCollectionTermGroup",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and includes the term group currently (if set) assigned to the site collection.",
        SortOrder = 5)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistComposedLookFiles",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and saves the files that make up the composed look to the same folder as where the template is saved.",
        SortOrder = 6)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -Handlers Lists, SiteSecurity",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, but only processes lists and site security when generating the template.",
        SortOrder = 7)]
    [CmdletExample(
        Code = @"
PS:> $handler1 = New-SPOExtensibilityHandlerObject -Assembly Contoso.Core.Handlers -Type Contoso.Core.Handlers.MyExtensibilityHandler1
PS:> $handler2 = New-SPOExtensibilityHandlerObject -Assembly Contoso.Core.Handlers -Type Contoso.Core.Handlers.MyExtensibilityHandler1
PS:> Get-SPOProvisioningTemplate -Out NewTemplate.xml -ExtensibilityHandlers $handler1,$handler2",
        Remarks = @"This will create two new ExtensibilityHandler objects that are run during extraction of the template",
        SortOrder = 8)]
#if !SP2013
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistMultiLanguageResources",
        Introduction = "Only supported on SP2016 and SP Online",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, and for supported artifacts it will create a resource file for each supported language (based upon the language settings of the current web). The generated resource files will be named after the value specified in the Out parameter. For instance if the Out parameter is specified as -Out 'template.xml' the generated resource file will be called 'template.en-US.resx'.",
        SortOrder = 9)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistMultiLanguageResources -ResourceFilePrefix MyResources",
        Introduction = "Only supported on SP2016 and SP Online",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, and for supported artifacts it will create a resource file for each supported language (based upon the language settings of the current web). The generated resource files will be named 'MyResources.en-US.resx' etc.",
        SortOrder = 10)]
#endif

    public class GetProvisioningTemplate : SPOWebCmdlet
    {
        [Parameter(Mandatory = false, Position = 0, HelpMessage = "Filename to write to, optionally including full path")]
        public string Out;

        [Parameter(Mandatory = false, Position = 1, HelpMessage = "The schema of the output to use, defaults to the latest schema")]
        public XMLPnPSchemaVersion Schema = XMLPnPSchemaVersion.LATEST;

        [Parameter(Mandatory = false, HelpMessage = "If specified, all term groups will be included. Overrides IncludeSiteCollectionTermGroup.")]
        public SwitchParameter IncludeAllTermGroups;

        [Parameter(Mandatory = false, HelpMessage = "If specified, all the site collection term groups will be included. Overridden by IncludeAllTermGroups.")]
        public SwitchParameter IncludeSiteCollectionTermGroup;

        [Parameter(Mandatory = false, HelpMessage = "If specified all site groups will be included.")]
        public SwitchParameter IncludeSiteGroups;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files used for masterpages, sitelogo, alternate CSS and the files that make up the composed look will be saved.")]
        public SwitchParameter PersistBrandingFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files making up the composed look (background image, font file and color file) will be saved.")]
        [Obsolete("Use PersistBrandingFiles instead.")]
        public SwitchParameter PersistComposedLookFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files used for the publishing feature will be saved.")]
        public SwitchParameter PersistPublishingFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified, out of the box / native publishing files will be saved.")]
        public SwitchParameter IncludeNativePublishingFiles;

#if !SP2013
        [Parameter(Mandatory = false, HelpMessage = "If specified, resource values for applicable artifacts will be persisted to a resource file")]
        public SwitchParameter PersistMultiLanguageResources;

        [Parameter(Mandatory = false, HelpMessage = "If specified, resource files will be saved with the specified prefix instead of using the template name specified. If no template name is specified the files will be called PnP-Resources.<language>.resx. See examples for more info.")]
        public string ResourceFilePrefix;
#endif
        [Parameter(Mandatory = false, HelpMessage = "Allows you to only process a specific type of artifact in the site. Notice that this might result in a non-working template, as some of the handlers require other artifacts in place if they are not part of what your extracting.")]
        public Handlers Handlers;

        [Parameter(Mandatory = false, HelpMessage = "Allows you to run all handlers, excluding the ones specified.")]
        public Handlers ExcludeHandlers;

        [Parameter(Mandatory = false, HelpMessage = "Allows you to specify ExtensbilityHandlers to execute while extracting a template")]
        public ExtensibilityHandler[] ExtensibilityHandlers;

        [Parameter(Mandatory = false, HelpMessage = "Overwrites the output file if it exists.")]
        public SwitchParameter Force;

        [Parameter(Mandatory = false, HelpMessage = "Exports the template without the use of a base template, causing all OOTB artifacts to be included. Using this switch is generally not required/recommended.")]
        [Obsolete("Use of this method is generally not required/recommended")]
        public SwitchParameter NoBaseTemplate;

        [Parameter(Mandatory = false)]
        public System.Text.Encoding Encoding = System.Text.Encoding.Unicode;



        protected override void ExecuteCmdlet()
        {
            if(PersistMultiLanguageResources == false && ResourceFilePrefix != null)
            {
                WriteWarning("In order to export resource files, also specify the PersistMultiLanguageResources switch");
            }
            if (!string.IsNullOrEmpty(Out))
            {
                if (!Path.IsPathRooted(Out))
                {
                    Out = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Out);
                }
                if (File.Exists(Out))<commit_msg>Added missing !SP2013 region to be able to build with debug15<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using OfficeDevPnP.Core.Utilities;
using SharePointPnP.PowerShell.Commands.Enums;
using File = System.IO.File;
using Resources = SharePointPnP.PowerShell.Commands.Properties.Resources;


namespace SharePointPnP.PowerShell.Commands.Branding
{
    [Cmdlet(VerbsCommon.Get, "SPOProvisioningTemplate", SupportsShouldProcess = true)]
    [CmdletHelp("Generates a provisioning template from a web",
        Category = CmdletHelpCategory.Branding)]
    [CmdletExample(
       Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp",
       Remarks = "Extracts a provisioning template in Office Open XML from the current web.",
       SortOrder = 1)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.xml",
       Remarks = "Extracts a provisioning template in XML format from the current web.",
       SortOrder = 2)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -Schema V201503",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and saves it in the V201503 version of the schema.",
        SortOrder = 3)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -IncludeAllTermGroups",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and includes all term groups, term sets and terms from the Managed Metadata Service Taxonomy.",
        SortOrder = 4)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -IncludeSiteCollectionTermGroup",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and includes the term group currently (if set) assigned to the site collection.",
        SortOrder = 5)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistComposedLookFiles",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web and saves the files that make up the composed look to the same folder as where the template is saved.",
        SortOrder = 6)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -Handlers Lists, SiteSecurity",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, but only processes lists and site security when generating the template.",
        SortOrder = 7)]
    [CmdletExample(
        Code = @"
PS:> $handler1 = New-SPOExtensibilityHandlerObject -Assembly Contoso.Core.Handlers -Type Contoso.Core.Handlers.MyExtensibilityHandler1
PS:> $handler2 = New-SPOExtensibilityHandlerObject -Assembly Contoso.Core.Handlers -Type Contoso.Core.Handlers.MyExtensibilityHandler1
PS:> Get-SPOProvisioningTemplate -Out NewTemplate.xml -ExtensibilityHandlers $handler1,$handler2",
        Remarks = @"This will create two new ExtensibilityHandler objects that are run during extraction of the template",
        SortOrder = 8)]
#if !SP2013
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistMultiLanguageResources",
        Introduction = "Only supported on SP2016 and SP Online",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, and for supported artifacts it will create a resource file for each supported language (based upon the language settings of the current web). The generated resource files will be named after the value specified in the Out parameter. For instance if the Out parameter is specified as -Out 'template.xml' the generated resource file will be called 'template.en-US.resx'.",
        SortOrder = 9)]
    [CmdletExample(
        Code = @"PS:> Get-SPOProvisioningTemplate -Out template.pnp -PersistMultiLanguageResources -ResourceFilePrefix MyResources",
        Introduction = "Only supported on SP2016 and SP Online",
        Remarks = "Extracts a provisioning template in Office Open XML from the current web, and for supported artifacts it will create a resource file for each supported language (based upon the language settings of the current web). The generated resource files will be named 'MyResources.en-US.resx' etc.",
        SortOrder = 10)]
#endif

    public class GetProvisioningTemplate : SPOWebCmdlet
    {
        [Parameter(Mandatory = false, Position = 0, HelpMessage = "Filename to write to, optionally including full path")]
        public string Out;

        [Parameter(Mandatory = false, Position = 1, HelpMessage = "The schema of the output to use, defaults to the latest schema")]
        public XMLPnPSchemaVersion Schema = XMLPnPSchemaVersion.LATEST;

        [Parameter(Mandatory = false, HelpMessage = "If specified, all term groups will be included. Overrides IncludeSiteCollectionTermGroup.")]
        public SwitchParameter IncludeAllTermGroups;

        [Parameter(Mandatory = false, HelpMessage = "If specified, all the site collection term groups will be included. Overridden by IncludeAllTermGroups.")]
        public SwitchParameter IncludeSiteCollectionTermGroup;

        [Parameter(Mandatory = false, HelpMessage = "If specified all site groups will be included.")]
        public SwitchParameter IncludeSiteGroups;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files used for masterpages, sitelogo, alternate CSS and the files that make up the composed look will be saved.")]
        public SwitchParameter PersistBrandingFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files making up the composed look (background image, font file and color file) will be saved.")]
        [Obsolete("Use PersistBrandingFiles instead.")]
        public SwitchParameter PersistComposedLookFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified the files used for the publishing feature will be saved.")]
        public SwitchParameter PersistPublishingFiles;

        [Parameter(Mandatory = false, HelpMessage = "If specified, out of the box / native publishing files will be saved.")]
        public SwitchParameter IncludeNativePublishingFiles;

#if !SP2013
        [Parameter(Mandatory = false, HelpMessage = "If specified, resource values for applicable artifacts will be persisted to a resource file")]
        public SwitchParameter PersistMultiLanguageResources;

        [Parameter(Mandatory = false, HelpMessage = "If specified, resource files will be saved with the specified prefix instead of using the template name specified. If no template name is specified the files will be called PnP-Resources.<language>.resx. See examples for more info.")]
        public string ResourceFilePrefix;
#endif
        [Parameter(Mandatory = false, HelpMessage = "Allows you to only process a specific type of artifact in the site. Notice that this might result in a non-working template, as some of the handlers require other artifacts in place if they are not part of what your extracting.")]
        public Handlers Handlers;

        [Parameter(Mandatory = false, HelpMessage = "Allows you to run all handlers, excluding the ones specified.")]
        public Handlers ExcludeHandlers;

        [Parameter(Mandatory = false, HelpMessage = "Allows you to specify ExtensbilityHandlers to execute while extracting a template")]
        public ExtensibilityHandler[] ExtensibilityHandlers;

        [Parameter(Mandatory = false, HelpMessage = "Overwrites the output file if it exists.")]
        public SwitchParameter Force;

        [Parameter(Mandatory = false, HelpMessage = "Exports the template without the use of a base template, causing all OOTB artifacts to be included. Using this switch is generally not required/recommended.")]
        [Obsolete("Use of this method is generally not required/recommended")]
        public SwitchParameter NoBaseTemplate;

        [Parameter(Mandatory = false)]
        public System.Text.Encoding Encoding = System.Text.Encoding.Unicode;



        protected override void ExecuteCmdlet()
        {
#if !SP2013
            if(PersistMultiLanguageResources == false && ResourceFilePrefix != null)
            {
                WriteWarning("In order to export resource files, also specify the PersistMultiLanguageResources switch");
            }
#endif
            if (!string.IsNullOrEmpty(Out))
            {
                if (!Path.IsPathRooted(Out))
                {
                    Out = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Out);
                }
                if (File.Exists(Out))
{'fraction_non_alphanumeric': 0.07205922676172197, 'fraction_numerical': 0.005703317795448313, 'mean_word_length': 3.695159629248198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5820986', 'n_tokens_mistral': 4736, 'n_tokens_neox': 4574, 'n_words': 1776}