swift build use of unresolved identifier - vapor

I used vapor to build a project. create a swift file using xcode.
structure like this:
├── Sources
│ └──App
│ │ └── Controllers
│ │ └── Models
│ │ │ └── File.swift
│ └──Run
│ └── main.swift
└── Package.swift
with main.swift
let config = try Config()
try config.setup()
let drop = try Droplet(config)
try drop.setup()
File.test()
try drop.run()
with File.swift
class File {
class func test() -> Void{
print("--\(self)--");
}
}
the above code xcode can run normally. but using the command swift build to get an error.
log:
Compile Swift Module 'App' (6 sources)
Compile Swift Module 'Run' (1 sources)
/Users/xxx/Documents/testServer/Sources/Run/main.swift:25:1: error: use of
unresolved identifier 'File'
File.test()
^~~~
CoreServices.cFile:1:12: note: did you mean 'cFile'?
public var cFile: OSType { get }
^
<unknown>:0: error: build had 1 command failures
error: exit(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/xxx/Documents/testServer/.build/debug.yamlhere

Your main.swift is in module Run, while File is in module App. To call a method of a class from another module, you have to perform the following:
Make the class (File) public
Make the method (test) public
Import the module in the file of the calling method - add import App to main.swift

Probably what is happening is that your class File is in the target 'App', while your Main is in target 'Run', so one can't see the other.
What you need to do is to add the line File.test() into Droplet+setup.swift file over setup() function, that you might have in your project located over the 'App' target.
Some thing like this:
#_exported import Vapor
extension Droplet {
public func setup() throws {
try setupRoutes()
// ADD YOUR CLASS CALL OVER HERE
File.test()
}
}

Related

How to resolve a dependency in an external package workspace_file?

Trying to use a target in the build_file from an external package imported through http_archive that has dependencies defined in the external package workspace via the workspace_file attribute fails. I'm using Bazel 0.27.0 on Debian Testing.
The documentation only talks about referencing targets in the provided build_file, but I could not find any information how one could reference a dependency defined in the provided workspace_file in the provided build_file.
The usual #stringtemplate3//jar syntax fails, but I don't know how I could include a reference to the imported archive which according to the manual would have to start with #antlr3_runtimes.
The project layout looks like this:
├── antlr.BUILD
├── antlr.WORKSPACE
├── BUILD
├── external_dependency
│ └── src
│ └── main
│ └── java
│ └── bazel
│ ├── BUILD
│ └── Hello.java
├── LICENSE
└── WORKSPACE
The WORKSPACE definition looks like this:
workspace(name="bazel")
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "antlr3_runtimes",
sha256 = "d4f7d3c38c5523f8009ff37528e5797c81adb454be6acc9af507cfcb41f2016f",
strip_prefix = "antlr3-master",
urls = ["https://github.com/ibre5041/antlr3/archive/master.tar.gz"],
build_file = "#//:antlr.BUILD",
workspace_file = "#//:antlr.WORKSPACE",
)
It seems that the provided workspace_file is not even analyzed. The build already fails when it tries to resolve the dependencies in the custom build file.
A repro can be found here: https://github.com/marcohu/bazel
bazel build //... shows this error message:
ERROR: /home/user/.cache/bazel/_bazel_user/64492308e78c9898c41f12c18dd29b63/external/antlr3_runtimes/BUILD.bazel:43:1: no such package '#stringtemplate3//jar': The repository '#stringtemplate3' could not be resolved and referenced by '#antlr3_runtimes//:antlr3_tool'
ERROR: Analysis of target '//external_dependency/src/main/java/bazel:hello' failed; build aborted: no such package '#stringtemplate3//jar': The repository '#stringtemplate3' could not be resolved
I reported this in the Bazel issue tracker, but it got rejected with a hint to post here.
Is this use case something that is just not possible? Or did I got the syntax wrong?
At least as of now (I suppose this statement may change in the future versions), bazel does not directly support transitive external dependencies. WORKSPACE file would still get read in even in your case and if it contained entirely broken syntax it'd still fail, but it does not get "acted upon" and you could (currently) for instance load from non-existing labels or call undefined functions and would still be left none the wiser for a nested WORKSPACE.
You essentially have two options:
Repeat your nested dependencies (http_archive rules) in your "parent"/top WORKSPACE.
You can define a function(s) with corresponding repository rules that you load and call in your "parent"/top WORKSPACE.
Bazel actually does support the transitive fetching of jvm dependencies. https://github.com/bazelbuild/rules_jvm_external
WORKSPACE
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
rules_jvm_external_tag = "2.0.1"
rules_jvm_external_sha = "55e8d3951647ae3dffde22b4f7f8dee11b3f70f3f89424713debd7076197eaca"
http_archive(
name = "rules_jvm_external",
sha256 = rules_jvm_external_sha,
strip_prefix = "rules_jvm_external-%s" % rules_jvm_external_tag,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % rules_jvm_external_tag,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
name = "maven",
artifacts = [
"io.grpc:grpc-netty-shaded:1.22.1",
"io.grpc:grpc-api:1.22.1",
"io.grpc:grpc-testing:1.22.1",
"io.grpc:grpc-core:1.22.1",
"junit:junit:4.12",
],
repositories = [
"https://jcenter.bintray.com/",
"https://repo1.maven.org/maven2",
],
)
BUILD
...
java_library(
name = "hyphenation-service",
srcs = ["src/test/java/com/example/hyphenation/HyphenationServiceTest.java"],
deps = ["#maven//:io_grpc_grpc_core"]
)
...
Example repository https://github.com/mancini0/bazel-grpc-playground

What does the "#" symbol mean in Bazel?

I'm studying Bazel building system at present. I always see the # symbol in Bazel script, but I cannot find any documentation about it. I searched it on the website of Bazel but the result seems useless.
# in Bazel.
For example:
filegroup(
name = "toolchain_fg",
srcs = [
":cc-compiler-amd64",
"#x86_64_unknown_linux_gnu_gcc_730//:compiler_components",
],
)
Could anybody explain the # symbol here for me?
This is to reference a remote repository.
From the doc, depending on other Bazel projects
local_repository(
name = "coworkers_project",
path = "/path/to/coworkers-project",
)
If your coworker has a target //foo:bar, your project can refer to it
as #coworkers_project//foo:bar.
See also the design doc of remote repository and bind example in workspace rules.
In Bazel, targets are referred by labels.
Bazel labels have the following format:
#repoName//packageName:target
For example, in the following packages found in myRepo:
myRepo
├── WORKSPACE
├── package1
│ └── BUILD
│ └── src
└── package2
├── BUILD
└── src
a target called myTarget in package1/BUILD can be labeled as as #myRepo//package1:myTarget globally.
If referenced from the same repo, for example from package2/BUILD, then the #myRepo prefix can be omitted and you can use //package1:myTarget.
If referenced from the same package, for example in another target from package1/BUILD, then the package name can be omitted and you can use :myTarget. The colon can also be omitted if it does not create confusion with a name. Such short form labels should not be confused with the names. Labels start with '//' or ':'. But names never do. For example, the name of the first package is package1 but its label is //package1.
Reference: https://docs.bazel.build/versions/master/build-ref.html

name conflicts of .so file in android binary

I'm linking a cc_library to an android_binary and getting a naming issue. Can someone tells me how to solve it?
The cc_library:
cc_library(
name = "native_library",
srcs = glob(["libs/**/*.so"])
)
The contents of libs directory:
libs
├── armeabi
│   ├── libSound.so
│   ├── libSec.so
│   ├── libWatch.so
│   └── libTec.so
├── armeabi-v7a
│   ├── libSound.so
│   ├── libSec.so
│   └── libWatch.so
├── x86
│   ├── libSound.so
│   ├── libSec.so
│   ├── libWatch.so
│   └── libTec.so
|—— other jars
And The error messages are like this:
ERROR: /the/path/to/BUILD:10:1: in android_binary rule //:debug_apk: Each library in the transitive closure must have a unique basename to avoid name collisions when packaged into an apk, but two libraries have the basename 'libSound.so': libs/armeabi/libSound.so and libs/armeabi-v7a/libSound.so.
...
An alternate approach that takes advantage of android_binary's --fat_apk_cpu flag and does not require renaming your libraries:
android_binary will build each cc_library once for each architecture specified by --fat_apk_cpu. The default of --fat_apk_cpu is just armeabi-v7a. This is known as the "Android split transition". When it builds each cc_library, that cc_library gets passed a --cpu flag from the list in --fat_apk_cpu. We can define config_setting rules that read these flags, and use a select statement in the cc_library, so that your cc_library contains different .so files depending on which architecture that it is built for.
For example:
# BUILD
CPUS = ["armeabi", "armeabi-v7a", "x86"]
[config_setting(name = cpu, values = {"cpu": cpu}) for cpu in CPUS]
cc_library(
name = "native_library",
srcs = select(
{":%s" % cpu : glob(["libs/%s/*.so" % cpu]) for cpu in CPUS}
),
)
android_binary(
name = "app",
srcs = glob(["*.java"]),
manifest = "AndroidManifest.xml",
deps = [":native_library"],
)
And then on the command line you can specify which architectures you want present in the final APK.
$ bazel build --fat_apk_cpu=armeabi,armeabi-v7a,x86 //:app
$ zipinfo -1 bazel-bin/app.apk | grep \.so$
lib/x86/libWatch.so
lib/x86/libSound.so
lib/x86/libSec.so
lib/x86/libTec.so
lib/armeabi-v7a/libWatch.so
lib/armeabi-v7a/libSound.so
lib/armeabi-v7a/libSec.so
lib/armeabi-v7a/libTec.so
lib/armeabi/libWatch.so
lib/armeabi/libSound.so
lib/armeabi/libSec.so
lib/armeabi/libTec.so
$ bazel build --fat_apk_cpu=x86 //:app
$ zipinfo -1 bazel-bin/app.apk | grep \.so$
lib/x86/libWatch.so
lib/x86/libSound.so
lib/x86/libSec.so
lib/x86/libTec.so
Specifying only one architecture to build for can speed up your development builds. For example, if you use an x86 emulator while you develop, you don't need the armeabi and armeabi-v7a .so files.
I might be wrong but it's the limitation of apk layout, so I'm afraid you just cannot have that named libs in a fat apk. Is renaming libs into libSound-armeabi.so etc an option for you?

Dart Test client- side HTML configuration

I understand that the package:unittest/unittest.dart is deprecated and the new package is package:test/test.dart.
Which are the equivalent of the library package:unittest/html_config.dart and the useHtmlConfiguration() function in the new test.dart framework.
Thanks.
Note: I am reading an outdated book ("Dart in Action"). So far I have been able to match the deprecated parts with the new standard parts of Dart.
Except now that I am reading the section of Unit-Test.
source code
.
├── PackList.dart
├── PackList.html
├── pubspec.lock
├── pubspec.yaml
├── styles.css
└── test
├── PackList_test.dart
└── PackList_test.html
I am trying to check if the value that the constructor return is not NULL.
import "package:test/test.dart";
import "../PackList.dart" as packListApp;
main() {
test("PackItem constructor", () {
var item = new packListApp.PackItem("Towel");
expect(item, isNotNull);
});
}
The source code works.
This is just an excersice to understand how the test framework works.
I expect item to be a new object.
With this properties initialized after var item = new packListApp.PackItem("Towel");
print(packItem.uiElement); //Towel
print(packItem.itemText); //div
The problem is that I don't know how do related the html part of my source code with the test.
When I run this test, I got this errors.
pub run test
00:00 +0 -1: loading test/PackList_test.dart [E]
Failed to load "test/PackList_test.dart":
Unable to spawn isolate: The built-in library 'dart:html' is not available on the stand-alone VM.
PackList.dart: line 1 pos 1: library handler failed
import "dart:html";
^
00:00 +0 -1: Some tests failed.
If I add #TestOn("dartium"), I got this message.
pub run test
No tests ran.
pub run test just runs VM tests. If you want to run browser tests, you need to explicitly tell it like
pub run test -pdartium
The my_test.html needs to contain
<link rel="x-dart-test" href="my_test.dart">
instead of a normal Dart script tag

Cordova 3.1.0 : Command Line interface not working

Cordova 3.1.0 : Command Line interface not working . I installed node.js and used th following command
$ sudo npm install -g cordova
after installation am getting following output :
/usr/local/bin/cordova -> /usr/local/lib/node_modules/cordova/bin/cordova
cordova#3.1.0-0.2.0 /usr/local/lib/node_modules/cordova
├── ncallbacks#1.0.0
├── semver#1.1.0
├── colors#0.6.2
├── open#0.0.3
├── mime#1.2.11
├── q#0.9.7
├── shelljs#0.1.2
├── follow-redirects#0.0.3 (underscore#1.5.2)
├── optimist#0.6.0 (wordwrap#0.0.2, minimist#0.0.5)
├── xcode#0.5.1 (node-uuid#1.3.3, pegjs#0.6.2)
├── glob#3.2.6 (inherits#2.0.1, minimatch#0.2.12)
├── tar#0.1.18 (inherits#2.0.1, block-stream#0.0.7, fstream#0.1.24)
├── elementtree#0.1.5 (sax#0.3.5)
├── prompt#0.2.7 (revalidator#0.1.5, pkginfo#0.3.0, read#1.0.5, utile#0.1.7, winston#0.6.2)
├── express#3.0.0 (methods#0.0.1, fresh#0.1.0, range-parser#0.0.4, debug#0.7.3, cookie#0.0.4, crc#0.2.0, commander#0.6.1, mkdirp#0.3.3, send#0.1.0, connect#2.6.0)
├── request#2.22.0 (json-stringify-safe#4.0.0, aws-sign#0.3.0, forever-agent#0.5.0, qs#0.6.5, tunnel-agent#0.3.0, oauth-sign#0.3.0, cookie-jar#0.3.0, node-uuid#1.4.1, http-signature#0.10.0, hawk#0.13.1, form-data#0.0.8)
├── npm#1.3.13
├── plist#0.4.3 (xmlbuilder#0.4.2, xmldom#0.1.16)
├── ripple-emulator#0.9.18 (connect-xcors#0.5.2, colors#0.6.0-1, accounting#0.3.2, request#2.12.0, moment#1.7.2, express#3.1.0)
└── plugman#0.14.0 (ncallbacks#1.1.0, osenv#0.0.3, bplist-parser#0.0.4, semver#2.0.11, underscore#1.4.4, nopt#1.0.10, dep-graph#1.1.0, xcode#0.6.1, rc#0.3.0, tar.gz#0.1.1, npm#1.3.4)
Not able to create add platform or create a project .
while using this command
$ cordova create hello com.example.hello HelloWorld
am getting the following error.
-bash: cordova: command not found
am using MAC OS
Did your PATH system variable include /usr/local/bin/?
You need to add the NPM directory into your PATH, so that you can run "cordova" from anywhere and it will get picked up.
For example, I had to add
;C:\Users\MBillau\AppData\Roaming\npm
which is where my npm folder is.

Resources