Ejemplo n.º 1
0
        // Builds a project to the provided file system, package, etc

        public async Task <BuildResult> BuildAsync()
        {
            var ct = new CancellationTokenSource();

            var sw = Stopwatch.StartNew();

            bool compiledTs = false;

            foreach (var file in package)
            {
                var format = FormatHelper.GetFormat(file);

                if (format == "scss")
                {
                    using (var compiledCss = await CompileCssAsync(file).ConfigureAwait(false))
                    {
                        var compiledName = file.Key.Replace(".scss", ".css");

                        if (compiledCss == null)
                        {
                            log.Info($"Skipped '{compiledName}' (is partial)");

                            continue;
                        }

                        log.Info($"Compiled '{compiledName}'");

                        var blob = new Blob(compiledName, compiledCss, new BlobProperties {
                            ContentType = "text/css"
                        });

                        await bucket.PutAsync(blob).ConfigureAwait(false);
                    }
                }
                else if (format == "ts")
                {
                    if (!compiledTs)
                    {
                        log.Info("Building TypeScript");

                        // This will copy over the typescript files
                        await tsProject.BuildAsync(ct.Token).ConfigureAwait(false);

                        compiledTs = true;
                    }

                    var compiledName = file.Key.Replace(".ts", ".js");

                    var outputPath = basePath + compiledName;

                    var fileStream = File.Open(outputPath, FileMode.Open, FileAccess.Read);

                    var blob = new Blob(compiledName, fileStream, new BlobProperties {
                        ContentType = "application/javascript"
                    });

                    log.Info($"Compiled '{compiledName}'");

                    await bucket.PutAsync(blob).ConfigureAwait(false);
                }
                else if (FormatHelper.IsStaticFormat(format))
                {
                    var blob = new Blob(
                        key: file.Key,
                        stream: await file.ToMemoryStreamAsync().ConfigureAwait(false)
                        );

                    // include content type?

                    await bucket.PutAsync(blob).ConfigureAwait(false);
                }
            }

            return(new BuildResult(BuildStatus.Completed, sw.Elapsed));
        }
Ejemplo n.º 2
0
        public async Task <WebLibrary> DeployAsync(IPackage package, RevisionSource source, int publisherId)
        {
            #region Preconditions

            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            #endregion

            var metadata = GetMetadata(package);

            var version = metadata.Version;

            // var existing = await registry.GetAsync(registry.Lookup(metadata.Name), metadata.Version);

            // if (existing != null) throw new PublishingConflict(existing);

            var mainPath = metadata.Main;

            var bowerFile = package.Find("bower.json");

            if (bowerFile != null)
            {
                try
                {
                    var bowerFileStream = await bowerFile.OpenAsync().ConfigureAwait(false);

                    var bower = PackageMetadata.Parse(bowerFileStream);

                    if (bower.Main != null)
                    {
                        mainPath = bower.Main;
                    }
                }
                catch { }
            }

            if (mainPath == null)
            {
                throw new Exception("A main property found in package.json or bower.json.");
            }

            var mainFile = package.Find(mainPath);

            if (mainFile == null)
            {
                throw new Exception($"The main file '{mainPath}' was not found");
            }

            var mainText = await mainFile.ReadAllTextAsync().ConfigureAwait(false);

            if (mainText.Length == 0)
            {
                throw new Exception($"{mainPath} is empty");
            }

            var mainBlobStream = new MemoryStream(Encoding.UTF8.GetBytes(mainText));

            var mainName = mainPath.Split('/').Last();

            if (!mainName.EndsWith(".js"))
            {
                throw new Exception($"Must end with js. was {mainName}");
            }

            var mainBlob = new Blob(
                key: $"libs/{metadata.Name}/{version}/{mainName}",
                stream: mainBlobStream,
                properties: new BlobProperties {
                ContentType = "application/javascript"
            }
                );

            var mainHash = Hash.ComputeSHA256(mainBlobStream);

            // Push to CDN
            await bucket.PutAsync(mainBlob).ConfigureAwait(false);

            if (metadata.Files != null)
            {
                // TODO: Copy over everything from files[] (excluding main)
                foreach (var fileName in metadata.Files)
                {
                    var fn = fileName;

                    if (fn.StartsWith("./"))
                    {
                        fn = fileName.Substring(2);
                    }

                    if (fn == mainPath)
                    {
                        continue;
                    }

                    var asset = package.Find(fn);

                    var format = asset.Key.Split('.').Last();

                    var n = asset.Key.Split('/').Last();

                    var ms = new MemoryStream();

                    using (var data = await asset.OpenAsync().ConfigureAwait(false))
                    {
                        data.CopyTo(ms);

                        ms.Position = 0;
                    }

                    var blob = new Blob(
                        key: $"libs/{metadata.Name}/{version}/{n}",
                        stream: ms,
                        properties: new BlobProperties {
                        ContentType = GetMime(format)
                    }
                        );

                    await bucket.PutAsync(blob).ConfigureAwait(false);
                }
            }

            var release = new WebLibrary(metadata.Name, version)
            {
                MainName   = mainName,
                MainSha256 = mainHash,
                Source     = source.ToString()
            };

            return(release);
        }