public async Task <ApiResult <bool> > SyncGithub(string id)
        {
            if (!ModelState.IsValid)
            {
                return(new ApiResult <bool>(l, BasicControllerEnums.UnprocessableEntity,
                                            ModelErrors()));
            }

            var githubConfiguration = await GetGithubOptions(id);

            if (githubConfiguration != null && !string.IsNullOrWhiteSpace(githubConfiguration.token))
            {
                if (githubConfiguration.syncLabels)
                {
                    await storageService.AddMessageAsync("apiresource-publish-github",
                                                         JsonConvert.SerializeObject(githubConfiguration));
                }

                if (githubConfiguration.syncDocs)
                {
                    await storageService.AddMessageAsync("apiresource-publish-github-readthedocs",
                                                         JsonConvert.SerializeObject(githubConfiguration));
                }
            }

            return(new ApiResult <bool>(true));
        }
        async Task <bool> ReleasePackage_NPM(string templateDirectory, Language lan, string SdkCode, string apiId)
        {
            #region 写SDK文件
            var fileName = string.Empty;

            switch (lan)
            {
            case Language.angular2:
                fileName = templateDirectory + "/index.ts";
                break;

            case Language.jQuery:
                fileName = templateDirectory + "/index.js";
                break;

            default:
                break;
            }

            using (var sw = new StreamWriter(fileName, false, Encoding.UTF8))
            {
                sw.WriteLine(SdkCode);
            }
            #endregion

            #region 更新包信息
            var configFilePath = templateDirectory + "/package.json";

            var options = await GetNpmOptions(lan, apiId);

            var JsonDoc = new JObject();

            if (!string.IsNullOrWhiteSpace(options.name))
            {
                JsonDoc["name"] = options.name;
            }

            if (!string.IsNullOrWhiteSpace(options.homepage))
            {
                JsonDoc["homepage"] = options.homepage;
            }

            if (!string.IsNullOrWhiteSpace(options.author))
            {
                JsonDoc["author"] = options.author;
            }

            if (options.keywords != null && options.keywords.Length > 0)
            {
                JsonDoc["keywords"] = JToken.FromObject(options.keywords);
            }

            if (!string.IsNullOrWhiteSpace(options.description))
            {
                JsonDoc["description"] = options.description;
            }

            if (string.IsNullOrWhiteSpace(options.version))
            {
                options.version = "0.0.0";
            }

            if (!string.IsNullOrWhiteSpace(options.license))
            {
                JsonDoc["license"] = options.license;
            }

            #region version

            var CurrentVersion = Version.Parse(options.version);

            var newVersion = string.Empty;

            if (CurrentVersion.Build + 1 < int.MaxValue)
            {
                newVersion = $"{CurrentVersion.Major}.{CurrentVersion.Minor}.{CurrentVersion.Build + 1}";
            }

            else if (CurrentVersion.Minor + 1 < int.MaxValue)
            {
                newVersion = $"{CurrentVersion.Major}.{CurrentVersion.Minor + 1}.{CurrentVersion.Build}";
            }

            else if (CurrentVersion.Major + 1 < int.MaxValue)
            {
                newVersion = $"{CurrentVersion.Major + 1}.{CurrentVersion.Minor}.{CurrentVersion.Build}";
            }

            JsonDoc["version"] = newVersion;
            #endregion

            using (var sw = new StreamWriter(configFilePath, false, Encoding.UTF8))
            {
                await sw.WriteLineAsync(JsonDoc.ToString());
            }
            #endregion

            var npmrcFilePath = templateDirectory + "/.npmrc";
            if (!string.IsNullOrWhiteSpace(options.token))
            {
                using (var sw = new StreamWriter(npmrcFilePath, false, Encoding.UTF8))
                {
                    await sw.WriteLineAsync("//registry.npmjs.org/:_authToken=" + options.token);
                }
            }

            var readmeFilePath = templateDirectory + "/README.md";
            if (!string.IsNullOrWhiteSpace(options.README))
            {
                var releaseREAME = options.README.Replace("<%SdkCode%>", SdkCode);

                using (var sw = new StreamWriter(readmeFilePath, false, Encoding.UTF8))
                {
                    await sw.WriteLineAsync(releaseREAME);
                }
            }

            #region 打包SDK文件为.zip
            var releaseDirectory = templateDirectory + "_release/";

            if (!Directory.Exists(releaseDirectory))
            {
                Directory.CreateDirectory(releaseDirectory);
            }

            var templateName    = Directory.GetParent(templateDirectory).Name;
            var languageName    = Path.GetFileName(templateDirectory);
            var packageFileName = $"{templateName}.{languageName}_{newVersion}.zip";

            ZipFile.CreateFromDirectory(templateDirectory, releaseDirectory + packageFileName);
            #endregion

            #region   .zip,发消息到发包队列
            var blobUrl = string.Empty;

            using (var zipFileStream = new FileStream(releaseDirectory + packageFileName, FileMode.Open, FileAccess.Read))
            {
                blobUrl = await storageService.UploadBlobAsync(zipFileStream, "codegen-npm", packageFileName);
            }

            await storageService.AddMessageAsync("publish-package-npm", blobUrl);

            #endregion

            #region 清理本地文件
            try
            {
                Directory.Delete(releaseDirectory, true);
                Directory.Delete(templateDirectory, true);
            }
            catch { }
            #endregion

            options.version = newVersion;

            await SetNpmOptions(apiId, lan, options);

            return(true);
        }
예제 #3
0
        public ApiResult <bool> Publish([FromBody] PublishRequest value)
        {
            var entity = sdkDB.Packages.Where(x => x.Id == value.id && x.UserID == UserId)
                         .Include(x => x.SdkGenerators).FirstOrDefault();

            if (entity == null)
            {
                return(new ApiResult <bool>(l, BasicControllerEnums.NotFound));
            }

            #region version
            Version ReleaseVersion = new Version(1, 0, 0);
            if (!string.IsNullOrWhiteSpace(value.Version))
            {
                ReleaseVersion = Version.Parse(value.Version);
            }
            engine.RemoveVariable("packageVersion");
            engine.SetVariableValue("packageVersion", ReleaseVersion.ToString());
            #endregion

            var SdkRootPath = $"./sdk/{value.id}/" + DateTime.Now.Ticks.ToString();

            var SdkBuildPath = $"{SdkRootPath}/build";

            if (!Directory.Exists(SdkBuildPath))
            {
                Directory.CreateDirectory(SdkBuildPath);
            }

            #region 生成软件包
            var Codes = BuildPackage(entity);

            foreach (var t in Codes)
            {
                using (var sw = new StreamWriter($"{SdkBuildPath}/{t.Key}", false, Encoding.UTF8))
                {
                    sw.WriteLine(t.Value);
                }
            }
            #endregion

            #region 打包软件包
            var SdkReleasePath = $"{SdkRootPath}/release";
            if (!Directory.Exists(SdkReleasePath))
            {
                Directory.CreateDirectory(SdkReleasePath);
            }

            var SdkRootName = Directory.GetParent(SdkRootPath).Name;
            var PackagePath = $"{SdkReleasePath}/{SdkRootName}.zip";
            ZipFile.CreateFromDirectory(SdkBuildPath, PackagePath);
            #endregion

            #region   软件包、加入发布计划
            var blobUrl = string.Empty;

            using (var zipFileStream = new FileStream(PackagePath, FileMode.Open, FileAccess.Read))
            {
                blobUrl = storageService.UploadBlobAsync(zipFileStream, "codegen-npm", entity.PackageName).Result;
            }

            var queueResult = storageService.AddMessageAsync("publish-package-npm", blobUrl).Result;
            #endregion

            #region 清理本地文件
            //try
            //{
            //    Directory.Delete(SdkRootPath, true);
            //}
            //catch { }
            #endregion

            #region 记录发布历史
            sdkDB.ReleaseHistories.Add(new SdkReleaseHistory()
            {
                Description  = value.Description,
                SdkPackageId = value.id,
                ReleaseDate  = DateTime.UtcNow.AddHours(8).ToString("G"),
                Remark       = value.Remark,
                Tags         = value.Tags,
                UserID       = UserId,
                Version      = ReleaseVersion.ToString()
            });
            #endregion

            try
            {
                sdkDB.SaveChanges();
            }

            catch (Exception ex)
            {
                return(new ApiResult <bool>(l, BasicControllerEnums.ExpectationFailed, ex.Message)
                {
                    data = false
                });
            }

            return(new ApiResult <bool>(true));
        }