Exemple #1
0
        public void Deploy(List <Argument> args)
        {
            assetsLocalPath = ArgumentHelper.Find("-AssetsLocalPath", args);
            bucketPath      = ArgumentHelper.Find("-BucketPath", args);
            awsAccessKey    = ArgumentHelper.Find("-AwsAccessKey", args);
            awsSecretKey    = ArgumentHelper.Find("-AwsSecretKey", args);
            awsRegion       = ArgumentHelper.Find("-AwsRegion", args);

            if (string.IsNullOrEmpty(assetsLocalPath) || string.IsNullOrEmpty(bucketPath) ||
                string.IsNullOrEmpty(awsAccessKey) ||
                string.IsNullOrEmpty(awsSecretKey) ||
                string.IsNullOrEmpty(awsRegion))
            {
                throw new Exception("The parameres are not defined. -AssetsLocalPath,-BucketPath,-AwsAccessKey,-AwsSecretKey,-AwsRegion");
            }

            var s3Client = new AmazonS3Client(new BasicAWSCredentials(awsAccessKey, awsSecretKey),
                                              RegionEndpoint.GetBySystemName(awsRegion));

            TransferUtility directoryTransferUtility = new TransferUtility(s3Client);

            directoryTransferUtility.UploadDirectory(new TransferUtilityUploadDirectoryRequest()
            {
                BucketName = bucketPath,
                Directory  = assetsLocalPath,
                UploadFilesConcurrently = true,
                CannedACL = S3CannedACL.PublicRead, SearchOption = SearchOption.AllDirectories
            });
        }
Exemple #2
0
        public void Minify(List <Argument> args)
        {
            appPath              = ArgumentHelper.Find("-AppPath", args);
            assetsLocalPath      = ArgumentHelper.Find("-AssetsLocalPath", args);
            assetsDevelopmentUrl = ArgumentHelper.Find("-AssetsDevUrl", args);
            assetsProductionUrl  = ArgumentHelper.Find("-AssetsProdUrl", args);

            if (string.IsNullOrEmpty(appPath) || string.IsNullOrEmpty(assetsLocalPath) ||
                string.IsNullOrEmpty(assetsDevelopmentUrl) ||
                string.IsNullOrEmpty(assetsProductionUrl))
            {
                throw new Exception("The parameres are not defined. -AppPath,-AssetsLocalPath,-AssetsDevUrl,-AssetsProdUrl, BundleFiles( optional, false is default)");
            }

            var hasBundleArg = ArgumentHelper.Find("-BundleFiles", args);

            if (hasBundleArg != null)
            {
                bool.TryParse(hasBundleArg, out bundleFiles);
            }

            var htmlFiles = Directory.GetFiles(appPath, "*.cshtml", SearchOption.AllDirectories);

            foreach (var htmlFile in htmlFiles)
            {
                string rawContent = File.ReadAllText(htmlFile, Encoding.UTF8);

                //Will change the url to work loccally
                rawContent = rawContent.Replace(assetsDevelopmentUrl, assetsLocalPath);

                if (bundleFiles)
                {
                    var document = new HtmlDocument()
                    {
                        OptionCheckSyntax = false, OptionWriteEmptyNodes = true
                    };
                    document.LoadHtml(rawContent);

                    var head = document.DocumentNode.SelectSingleNode("/html/head");

                    if (head != null)
                    {
                        BundleAssets(ref rawContent, head);
                    }

                    var body = document.DocumentNode.SelectSingleNode("/html/body") ?? document.DocumentNode;

                    BundleAssets(ref rawContent, body);
                }

                rawContent = rawContent.Replace(assetsLocalPath + $@"\{bundleFolderName}\", assetsLocalPath + $@"/{bundleFolderName}/");
                rawContent = rawContent.Replace(assetsLocalPath, assetsDevelopmentUrl);
                rawContent = rawContent.Replace(assetsDevelopmentUrl, assetsProductionUrl);

                var htmlCompressor = (HtmlCompressor)Minifiers.HtmlAdvanced;
                htmlCompressor.RemoveComments     = true;
                htmlCompressor.PreserveLineBreaks = true;
                rawContent = htmlCompressor.Compress(rawContent);
                File.WriteAllText(htmlFile, rawContent, Encoding.UTF8);
            }
        }