-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gazelle): generate kt_jvm_binary targets (#2990)
GitOrigin-RevId: 762169dfe192a8c5ac31f269fe59908f335ea078
- Loading branch information
Showing
25 changed files
with
229 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,73 @@ | ||
package gazelle | ||
|
||
import "strings" | ||
import ( | ||
"path" | ||
"strings" | ||
) | ||
|
||
import "github.com/emirpasic/gods/sets/treeset" | ||
|
||
func IsNativeImport(impt string) bool { | ||
return strings.HasPrefix(impt, "kotlin.") || strings.HasPrefix(impt, "kotlinx.") || strings.HasPrefix(impt, "java.") || strings.HasPrefix(impt, "javax.") | ||
} | ||
|
||
type KotlinTarget struct { | ||
Imports *treeset.Set | ||
} | ||
|
||
/** | ||
* Information for kotlin library target including: | ||
* - BUILD package name | ||
* - kotlin files | ||
* - kotlin import statements from all files | ||
* - kotlin packages implemented | ||
* - kotlin files with main() methods | ||
*/ | ||
type KotlinTarget struct { | ||
Name string | ||
type KotlinLibTarget struct { | ||
KotlinTarget | ||
|
||
Imports *treeset.Set | ||
Packages *treeset.Set | ||
|
||
Mains *treeset.Set | ||
|
||
Files *treeset.Set | ||
Files *treeset.Set | ||
} | ||
|
||
func NewKotlinTarget() *KotlinTarget { | ||
return &KotlinTarget{ | ||
Imports: treeset.NewWith(importStatementComparator), | ||
func NewKotlinLibTarget() *KotlinLibTarget { | ||
return &KotlinLibTarget{ | ||
KotlinTarget: KotlinTarget{ | ||
Imports: treeset.NewWith(importStatementComparator), | ||
}, | ||
Packages: treeset.NewWithStringComparator(), | ||
Mains: treeset.NewWithStringComparator(), | ||
Files: treeset.NewWithStringComparator(), | ||
} | ||
} | ||
|
||
/** | ||
* Information for kotlin binary (main() method) including: | ||
* - kotlin import statements from all files | ||
* - the package | ||
* - the file | ||
*/ | ||
type KotlinBinTarget struct { | ||
KotlinTarget | ||
|
||
File string | ||
Package string | ||
} | ||
|
||
func NewKotlinBinTarget(file, pkg string) *KotlinBinTarget { | ||
return &KotlinBinTarget{ | ||
KotlinTarget: KotlinTarget{ | ||
Imports: treeset.NewWith(importStatementComparator), | ||
}, | ||
File: file, | ||
Package: pkg, | ||
} | ||
} | ||
|
||
// packagesKey is the name of a private attribute set on generated kt_library | ||
// rules. This attribute contains the KotlinTarget for the target. | ||
const packagesKey = "_java_packages" | ||
const packagesKey = "_kotlin_package" | ||
|
||
func toBinaryTargetName(mainFile string) string { | ||
base := strings.ToLower(strings.TrimSuffix(path.Base(mainFile), path.Ext(mainFile))) | ||
|
||
// TODO: move target name template to directive | ||
return base + "_bin" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_binary", "kt_jvm_library") | ||
|
||
kt_jvm_library( | ||
name = "bin", | ||
srcs = ["lib.kt"], | ||
) | ||
|
||
kt_jvm_binary( | ||
name = "hello_bin", | ||
srcs = ["Hello.kt"], | ||
main_class = "Hello", | ||
) | ||
|
||
kt_jvm_binary( | ||
name = "pkghello_bin", | ||
srcs = ["PkgHello.kt"], | ||
main_class = "foo.pkg.PkgHello", | ||
deps = [":bin"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fun main() { | ||
println("Hello world!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package foo.pkg | ||
|
||
import test.lib.* | ||
|
||
fun main() { | ||
println("Hello world from ", Constants.NAME) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# This is a Bazel workspace for the Gazelle test data. | ||
workspace(name = "bin") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package test.lib | ||
|
||
object Constants { | ||
const val NAME = "bin test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package(default_visibility = ["//visibility:public"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.