From 111aaa3e211463c5a7bc16d664c0053deecfb2a3 Mon Sep 17 00:00:00 2001 From: Joshua Newman Date: Sat, 23 Jun 2012 18:26:33 -0500 Subject: [PATCH 1/2] Reverted previous commits (decided a branch makes more sense). Details: - Added a basic decorator for package.json files. - Added a Repository interface, which should know how to download its sources. - Added a Github implementation of Repository, which is probably not completely working. --- .../eriwen/gradle/js/commonJs/Package.groovy | 87 +++++++++++++++++++ .../gradle/js/commonJs/Repository.groovy | 20 +++++ .../repositories/GithubRepository.groovy | 52 +++++++++++ 3 files changed, 159 insertions(+) create mode 100644 src/main/groovy/com/eriwen/gradle/js/commonJs/Package.groovy create mode 100644 src/main/groovy/com/eriwen/gradle/js/commonJs/Repository.groovy create mode 100644 src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy diff --git a/src/main/groovy/com/eriwen/gradle/js/commonJs/Package.groovy b/src/main/groovy/com/eriwen/gradle/js/commonJs/Package.groovy new file mode 100644 index 0000000..930fb9e --- /dev/null +++ b/src/main/groovy/com/eriwen/gradle/js/commonJs/Package.groovy @@ -0,0 +1,87 @@ +/* + * Copyright 2012 Joshua Newman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.eriwen.gradle.js.commonJs + +import groovy.json.JsonSlurper + +/** + * Decorates a path to a package.json file w/ property accessors and + */ +class Package { + // The portion added to a github url before a tag to get a .zip + private final static ZIP_NAME = "zipball" + + private String path + private String delegate + private Object data; + private List repositories + + /** + * @param path The path to a package.json file. @see http://wiki.commonjs.org/wiki/Packages/1.0 + */ + Package(String path) { + this.path = path.trim() + this.delegate = path.toURL().getText() + this.data = new JsonSlurper().parseText(this.delegate) + } + + def getProperty(String name) { + if (name == 'repositories') { + return this.repositories != null ? this.repositories : this.getRepositories(); + } + else if (name == 'folder') { + return this.getFolder() + } + else if (name in this.data) { + return this.data[name] + } + else { + return this.delegate.getProperty(name) + } + } + + /** + * A lot of projects don't properly implement the CommonJS spec, so we'll assume the + * package.json is at the root of any repository + * + * @return collection of at least one valid repositories. + */ + List getRepositories() { + if ("repositories" in this.data) { + return this.repositories = this.data.repositories + } else { + return this.repositories = [ + [ + type: "git", // TODO how should I know if it's a git repo? + // Replacing the package.json, which I'll assume is in the root. + url: this.packagePathToRepo(this.path) + ] + ] + } + } + + /** + * + * @param packagePath Full path to a package.json + * @return + */ + protected String packagePathToRepo(String packagePath) { + // Trim off the package.json, then remove master for git repos. + // TODO find a better way to get from a repo to its root. + // TODO Might need a lot of these sort of replacements. + return packagePath.replaceAll('package.json$', '').replaceAll('/?master/?$', '.git') + } +} \ No newline at end of file diff --git a/src/main/groovy/com/eriwen/gradle/js/commonJs/Repository.groovy b/src/main/groovy/com/eriwen/gradle/js/commonJs/Repository.groovy new file mode 100644 index 0000000..7367835 --- /dev/null +++ b/src/main/groovy/com/eriwen/gradle/js/commonJs/Repository.groovy @@ -0,0 +1,20 @@ +/* + * Copyright 2012 Vodori Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.eriwen.gradle.js.commonJs + +interface Repository { + abstract File getTar(int index); + abstract File getZip(int index); +} diff --git a/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy b/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy new file mode 100644 index 0000000..b82d611 --- /dev/null +++ b/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy @@ -0,0 +1,52 @@ +/* + * Copyright 2012 Vodori Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.eriwen.gradle.js.commonJs.repositories + +import groovy.json.JsonSlurper +import com.eriwen.gradle.js.commonJs.Repository +import com.eriwen.gradle.js.commonJs.Package + +class GithubRepository implements Repository { + private Package pack + + GithubRepository(Package pack) { + this.pack = pack + } + + File getTar(int index=0) { + Object tag = getTagByName(pack.version) + return new File((tag.getAt("zipball_url") as String).toURL().getText()) + } + + File getZip(int index=0) { + Object tag = getTagByName(pack.version) + return new File((tag.getAt("tarball_url") as String).toURL().getText()) + } + + Object getTagByName(String name) { + // SEE: http://developer.github.com/v3/ + // TODO: This could be a whole lot nicer. + String tagJson = "https://api.github.com/repos/${userName}/$repoName/tags".toURL().getText() + def tags = new JsonSlurper().parseText(tagJson) + + for (Object tag in tags) { + if (tag.name == name) { + return tag + } + } + + return null + } +} From 27a5b314ff6433591b9afb97fbca2415a5d5d425 Mon Sep 17 00:00:00 2001 From: Joshua Newman Date: Sun, 24 Jun 2012 18:36:01 -0500 Subject: [PATCH 2/2] I should've updated some of my string manipulations when I moved the module. --- .../gradle/js/commonJs/repositories/GithubRepository.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy b/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy index b82d611..eff88d1 100644 --- a/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy +++ b/src/main/groovy/com/eriwen/gradle/js/commonJs/repositories/GithubRepository.groovy @@ -20,9 +20,13 @@ import com.eriwen.gradle.js.commonJs.Package class GithubRepository implements Repository { private Package pack + private String repoName GithubRepository(Package pack) { this.pack = pack + // Need this for the api. getting from the end, so I don't have to care if the scheme is + // present. + this.repoName = this.pack.repositories[0].url.split('/')[-3] } File getTar(int index=0) { @@ -38,7 +42,7 @@ class GithubRepository implements Repository { Object getTagByName(String name) { // SEE: http://developer.github.com/v3/ // TODO: This could be a whole lot nicer. - String tagJson = "https://api.github.com/repos/${userName}/$repoName/tags".toURL().getText() + String tagJson = "https://api.github.com/repos/${repoName}/$name/tags".toURL().getText() def tags = new JsonSlurper().parseText(tagJson) for (Object tag in tags) {