Skip to content

Commit

Permalink
Merge branch 'commonJs' of https://github.com/jnewman/gradle-js-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Jul 2, 2012
2 parents d82ce89 + 81dc572 commit e20cec5
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/groovy/com/eriwen/gradle/js/commonJs/Package.groovy
Original file line number Diff line number Diff line change
@@ -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')
}
}
20 changes: 20 additions & 0 deletions src/main/groovy/com/eriwen/gradle/js/commonJs/Repository.groovy
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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
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) {
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/${repoName}/$name/tags".toURL().getText()
def tags = new JsonSlurper().parseText(tagJson)

for (Object tag in tags) {
if (tag.name == name) {
return tag
}
}

return null
}
}

0 comments on commit e20cec5

Please sign in to comment.