我正在尝试创建自己的Julia包。
首先,我使用beforeValue
创建包:
fun main() {
val model = Model(File("file.txt"))
val gson = GsonBuilder().registerTypeAdapter(File::class.java, Adapter()).create()
// System.out just for PoC, don't try with large files because output will be massive
gson.toJson(model, System.out)
}
data class Model(@SerializedName("file") val file: File)
class Adapter : TypeAdapter<File>() {
override fun write(out: JsonWriter, value: File) {
Base64InputStream(FileInputStream(value), true, 0, null).use {
out.writeFromStream(it)
}
}
override fun read(`in`: JsonReader): File {
throw UnsupportedOperationException()
}
// JsonWriter only offers value(String), which would need you to load the whole file to the memory.
private fun JsonWriter.writeFromStream(inputStream: InputStream) {
val declaredField = javaClass.getDeclaredField("out")
val deferredName = javaClass.getDeclaredMethod("writeDeferredName")
val beforeValue = javaClass.getDeclaredMethod("beforeValue")
declaredField.isAccessible = true
deferredName.isAccessible = true
beforeValue.isAccessible = true
val actualWriter = declaredField.get(this) as Writer
deferredName.invoke(this)
beforeValue.invoke(this)
actualWriter.write("\"")
for (byte in inputStream.buffered()) {
actualWriter.write(byte.toInt())
}
actualWriter.write("\"")
}
}
然后我generate
打包,以便添加一些依赖项:
(v1.0) pkg> generate Example
Generating project Example:
Example/Project.toml
Example/src/Example.jl
现在,我想回到默认环境。我尝试了以下命令:
activate
但是现在看来我的环境是空的!发生了什么事?
答案 0 :(得分:3)
要返回默认环境,请使用不带参数的(v1.0) pkg> activate Example
Activating environment at `~/Example/Project.toml`
(Example) pkg> add Unicode
Resolving package versions...
Updating `~/Example/Project.toml`
[4ec0a83e] + Unicode
Updating `~/Example/Manifest.toml`
[4ec0a83e] + Unicode
:
(v1.0) pkg> activate v1.0
Activating new environment at `~/v1.0/Project.toml`
请注意,反馈与失败的尝试不同:打印的路径不同。
activate
将参数解释为文件系统路径。因此,(Example) pkg> activate
Activating environment at `~/.julia/environments/v1.2/Project.toml`
激活了一个新环境,将其存储在activate
上,这不是本意。
您可以使用内置的pkg> activate v1.0
命令来进一步了解~/v1.0/Project.toml
:
activate