From 29c9cf8ce70f8ea100c2215861b6bdd30a7c3992 Mon Sep 17 00:00:00 2001 From: Daniil Yarancev Date: Thu, 10 Aug 2017 12:51:16 +0300 Subject: [PATCH] Removed deprecated warnings, also tiny refactoring --- src/nimble.nim | 2 +- src/nimblepkg/nimscriptsupport.nim | 6 ++-- src/nimblepkg/packageinfo.nim | 3 +- src/nimblepkg/publish.nim | 52 ++++++++++++++---------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 6770694..8bb879a 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -1089,7 +1089,7 @@ when isMainModule: let currentExc = (ref NimbleError)(getCurrentException()) (error, hint) = getOutputInfo(currentExc) except NimbleQuit: - nil + discard finally: removeDir(getNimbleTempDir()) diff --git a/src/nimblepkg/nimscriptsupport.nim b/src/nimblepkg/nimscriptsupport.nim index b3ad381..6bcf763 100644 --- a/src/nimblepkg/nimscriptsupport.nim +++ b/src/nimblepkg/nimscriptsupport.nim @@ -8,7 +8,7 @@ import compiler/ast, compiler/modules, compiler/passes, compiler/passaux, compiler/condsyms, compiler/sem, compiler/semdata, compiler/llstream, compiler/vm, compiler/vmdef, compiler/commands, - compiler/msgs, compiler/magicsys, compiler/lists, compiler/idents, + compiler/msgs, compiler/magicsys, compiler/idents, compiler/nimconf from compiler/scriptconfig import setupVM @@ -228,7 +228,7 @@ proc execScript(scriptName: string, flags: Flags, options: Options): PSym = let tmpNimscriptApiPath = getTempDir() / "nimblepkg" / "nimscriptapi.nim" createDir(tmpNimscriptApiPath.splitFile.dir) writeFile(tmpNimscriptApiPath, nimscriptApi) - appendStr(searchPaths, getTempDir()) + searchPaths.add(getTempDir()) initDefines() loadConfigs(DefaultConfig) @@ -241,7 +241,7 @@ proc execScript(scriptName: string, flags: Flags, options: Options): PSym = registerPass(semPass) registerPass(evalPass) - appendStr(searchPaths, compiler_options.libpath) + searchPaths.add(compiler_options.libpath) when declared(resetAllModulesHard): result = makeModule(scriptName) diff --git a/src/nimblepkg/packageinfo.nim b/src/nimblepkg/packageinfo.nim index e85fcd1..7f6e8e6 100644 --- a/src/nimblepkg/packageinfo.nim +++ b/src/nimblepkg/packageinfo.nim @@ -186,7 +186,8 @@ proc fetchList*(list: PackageList, options: Options) = priority = LowPriority) try: - downloadFile(url, tempPath, proxy = proxy) + let client = newHttpClient(proxy = proxy) + client.downloadFile(url, tempPath) except: let message = "Could not download: " & getCurrentExceptionMsg() display("Warning:", message, Warning) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index a0bca66..96251d9 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -13,18 +13,22 @@ type user: string pw: string token: string ## base64 encoding of user:pw + http: HttpClient ## http client for doing API requests const ApiKeyFile = "github_api_token" ApiTokenEnvironmentVariable = "NIMBLE_GITHUB_API_TOKEN" + ReposUrl = "https://api.github.com/repos/" proc userAborted() = raise newException(NimbleError, "User aborted the process.") -proc createHeaders(a: Auth): string = - (("Authorization: token $1\c\L" % a.token) & - "Content-Type: application/x-www-form-urlencoded\c\L" & - "Accept: */*\c\L") +proc createHeaders(a: Auth) = + a.http.headers = newHttpHeaders({ + "Authorization": "token $1" % a.token, + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "*/*" + }) proc requestNewToken(cfg: Config): string = display("Info:", "Please create a new personal access token on Github in" & @@ -47,7 +51,7 @@ proc requestNewToken(cfg: Config): string = return token proc getGithubAuth(cfg: Config): Auth = - + result.http = newHttpClient() # always prefer the environment variable to asking for a new one if existsEnv(ApiTokenEnvironmentVariable): result.token = getEnv(ApiTokenEnvironmentVariable) @@ -63,9 +67,8 @@ proc getGithubAuth(cfg: Config): Auth = priority = HighPriority) except IOError: result.token = requestNewToken(cfg) - - let resp = getContent("https://api.github.com/user", - extraHeaders=createHeaders(result)).parseJson() + createHeaders(result) + let resp = result.http.getContent("https://api.github.com/user").parseJson() result.user = resp["login"].str display("Success:", "Verified as " & result.user, Success, HighPriority) @@ -78,8 +81,7 @@ proc isCorrectFork(j: JsonNode): bool = proc forkExists(a: Auth): bool = try: - let x = getContent("https://api.github.com/repos/" & a.user & "/packages", - extraHeaders=createHeaders(a)) + let x = a.http.getContent(ReposUrl & a.user & "/packages") let j = parseJson(x) result = isCorrectFork(j) except JsonParsingError, IOError: @@ -87,16 +89,14 @@ proc forkExists(a: Auth): bool = proc createFork(a: Auth) = try: - discard postContent("https://api.github.com/repos/nim-lang/packages/forks", - extraHeaders=createHeaders(a)) + discard a.http.postContent(ReposUrl & "nim-lang/packages/forks") except HttpRequestError: raise newException(NimbleError, "Unable to create fork. Access token" & " might not have enough permissions.") proc createPullRequest(a: Auth, packageName, branch: string) = display("Info", "Creating PR", priority = HighPriority) - discard postContent("https://api.github.com/repos/nim-lang/packages/pulls", - extraHeaders=createHeaders(a), + discard a.http.postContent(ReposUrl & "nim-lang/packages/pulls", body="""{"title": "Add package $1", "head": "$2:$3", "base": "master"}""" % [packageName, a.user, branch]) @@ -139,14 +139,15 @@ proc cleanupWhitespace(s: string): string = proc editJson(p: PackageInfo; url, tags, downloadMethod: string) = var contents = parseFile("packages.json") doAssert contents.kind == JArray - contents.add(%{ - "name": %p.name, - "url": %url, - "method": %downloadMethod, - "tags": %tags.split(), - "description": %p.description, - "license": %p.license, - "web": %url}) + contents.add(%*{ + "name": p.name, + "url": url, + "method": downloadMethod, + "tags": tags.split(), + "description": p.description, + "license": p.license, + "web": url + }) writeFile("packages.json", contents.pretty.cleanupWhitespace) proc getPackageOriginUrl(a: Auth): string = @@ -228,9 +229,4 @@ proc publish*(p: PackageInfo, o: Options) = display("Pushing", "to remote of fork.", priority = HighPriority) doCmd("git push " & getPackageOriginUrl(auth) & " " & branchName) createPullRequest(auth, p.name, branchName) - display("Success:", "Pull request successful.", Success, HighPriority) - -when isMainModule: - import packageinfo - var p = getPkgInfo(getCurrentDir()) - publish(p) + display("Success:", "Pull request successful.", Success, HighPriority) \ No newline at end of file