Skip to content

Commit

Permalink
🎉 init: 做完啦
Browse files Browse the repository at this point in the history
  • Loading branch information
2439905184 committed Aug 4, 2022
1 parent 58be0e2 commit cb0e25f
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.exe
test/
example/PocketPlugin/output/
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# gdap-generater-nim
gdap配置文件生成器的nim语言移植版

# 使用说明
请打开命令行
1. 创建工程模板 \
init.exe projectName
2. 将文件放进去(参考example文件夹)\
gen.exe projectName \
可选参数 -v 或者--verbose (用于输出详细信息)
# 从源码构建
## linux用户请从源码构建
```
nim c -d:release init.nim
nim c -d:release gen.nim
```
## 32位请从源码构建
```
nim c -d:release init.nim
nim c -d:release gen.nim
```
2 changes: 2 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nim c -d:release init.nim
nim c -d:release gen.nim
1 change: 1 addition & 0 deletions example.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gen.exe example/PocketPlugin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions example/PocketPlugin/dependencies/remote/remotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"custom_maven_repos": [],
"remotes": ["androidx.appcompat:appcompat:1.0.0","androidx.legacy:legacy-support-v4:1.0.0","com.squareup.okio:okio:1.14.0","com.squareup.okhttp3:okhttp:3.11.0","com.google.code.gson:gson:2.8.2","pl.droidsonroids.gif:android-gif-drawable:1.2.6","com.squareup.picasso:picasso:2.5.2"]
}
Binary file added example/PocketPlugin/plugin/PocketPlugin.aar
Binary file not shown.
1 change: 1 addition & 0 deletions example/PocketPlugin/plugin/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"PocketPlugin.aar"}
135 changes: 135 additions & 0 deletions gen.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os
import std/json
import std/sequtils
import std/parsecfg
import std/strutils
import system
import system/io

#是否启用详细输出
var verbose_enabled = false
var localDependencies:seq[string]
var remoteDependencies:seq[string]
var pluginDir = "/plugin"
var localDepDir = "/dependencies/local"
var remoteDepDir = "/dependencies/remote"
var outPutDir = "/output"

#插件的文件名字 xxx.aar
proc get_plugin(path:string):string =
var json_path = path & pluginDir & "/config.json"
var ConfigFile = open(json_path,fmRead)
var json_node = parseJson(ConfigFile.readAll())
ConfigFile.close()
var plugin = json_node["name"].getStr()
if verbose_enabled:
echo("正在获取config.json数据!")
echo(json_node)
echo "任务:get_plugin:获取插件文件名称->完成"
return plugin

#获得remotes.json里的所有内容
proc getRemotesJson(path:string):JsonNode =
var p = path & remoteDepDir & "/remotes.json"
var File = open(p,fmRead)
var json_node = parseJson(File.readAll())
File.close()
if verbose_enabled:
echo "正在获取remotes.json数据!"
echo json_node
echo "任务:getRemotesJson:获取remotes.json数据->完成"
return json_node

#把aar插件本体复制过去
proc copyLocalPlugin(path:string, plugin_name:string) =
var src = path & pluginDir & "/" & plugin_name
var dst = path & outPutDir & "/" & plugin_name
copyFile(src,dst)
echo "任务:copyLocalPlugin:复制本地插件aar->完成"

proc copyLocalDep(path:string) =
var p = path & localDepDir
var targetDir = path & outPutDir
var files = toSeq(walkFiles(p & "/*.aar"))
for file in files:
var file_name = extractFilename(file)
var target_file = targetDir & "/" & file_name
localDependencies.add(file_name)
if verbose_enabled:
echo "复制文件:" & file & "->" & target_file
copyFile(file,target_file)
echo "任务:copyLocalDep:复制本地依赖aar->完成"

proc writePluginConfig(path:string,plugin_name:string,remotes:JsonNode) =
var gdap = path & outPutDir & "/build.ini"
var GdapFile = open(gdap,fmReadWrite)
var dict = newConfig()
var name = plugin_name.split(".aar")[0]
#将序列数组转换为字符串
var mavens = $remotes["custom_maven_repos"].getElems()
var remote_libs = $remotes["remotes"].getElems()
var local_deps = $localDependencies
#这两个字符串变量需要进一步处理 去掉引号
mavens = mavens.split("@")[1]
remote_libs = remote_libs.split("@")[1]
local_deps = local_deps.split("@")[1]
dict.setSectionKey("config","name",name)
dict.setSectionKey("config","binary_type","local")
dict.setSectionKey("config","binary",plugin_name)
dict.setSectionKey("config","","")
dict.setSectionKey("dependencies","custom_maven_repos",mavens)
dict.setSectionKey("dependencies","local",local_deps)
dict.setSectionKey("dependencies","remote",remote_libs)
if verbose_enabled:
echo "正在对配置文件进行预构建操作!"
echo dict
GdapFile.write(dict)
GdapFile.close()
echo "任务:weitrPluginConfig:预构建配置文件->完成"

#修一下gdap文件
proc fix_gdap(path:string, plugin_name:string) =
var str = ""
var wrong_gdap = path & outPutDir & "/build.ini"
#echo wrong_gdap
for line in lines(wrong_gdap):
#echo line
var value = line.split("=")
if len(value) == 2:
var t = value[1].strip(leading = true,trailing = true,chars={'\"'})
var tmp = value[0] & "=" & t
str &= tmp & "\n"
else:
str &= line & "\n"
if verbose_enabled:
echo "正在修复gdap字符串数据!"
echo str
var gdap = path & outPutDir & "/build.gdap"
writefile(gdap,str)
echo "任务:fix_gdap:修复gdap配置文件->完成"

#清除错误的gdap配置文件
proc clean_up(path:string) =
remove_file(path & outPutDir & "/build.ini")
echo "任务:clean_up:清理错误配置文件完成"

proc genGdap(path:string) =
var output = path & "/output"
createDir(output)
var plugin_name = get_plugin(path)
var remotes:JsonNode = getRemotesJson(path)
copyLocalPlugin(path,plugin_name)
copyLocalDep(path)
writePluginConfig(path,plugin_name,remotes)
fix_gdap(path,plugin_name)
clean_up(path)

#程序入口
var cmd = commandLineParams()
var project_path = cmd[0]
if len(cmd) == 2:
if cmd[1] == "-v" or cmd[1] == "--verbose":
verbose_enabled = true
else:
echo "错误!不支持的参数!"
genGdap(project_path)

0 comments on commit cb0e25f

Please sign in to comment.