我是靠谱客的博主 整齐奇异果,最近开发中收集的这篇文章主要介绍Groovy读取文件夹目录结构,并生成.sql文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最近编辑于2019年1月18日

最近在读一个讲解flutter基本组件,项目地址https://github.com/alibaba/flutter-go?utm_source=androidweekly.io&utm_medium=website,当然想着一边看一边抄才能有所了解。但是在抄的过程中发现,他主要基于一个数据库文件来展示各个widget之间的组织结构的,那我如果直接把它的app.db复制到我的工程中,那是不是会有问题(因为我的工程一个widget还没有写啊)?
这是原demo效果:
             
他有三块,分别对应着
      
可以看到他并不是components、elements、themes的顺序,而是element、components、themes的顺序。我将他数据表打开可以看到他是这样的结构:
      
 

如果我要抄他的demo,我就需要每写一个demo,改一下数据库,那样有点麻烦哎。于是我想我能不能用代码生成这个数据库文件呢。
我的思路是创建一个空的app.db文件,然后使用sqlite可视化工具执行生成的.sql脚本,然后用这个新的app.db文件替代原来的app.db文件。那么问题就变成如何生成这个.sql脚本。
我们先来看一看原来app.db文件执行了什么样的sql语句,通过Navicat Premium工具导出main.sql文件内容如下:

/*
 Navicat Premium Data Transfer

 Source Server         : app
 Source Server Type    : SQLite
 Source Server Version : 3008008
 Source Database       : main

 Target Server Type    : SQLite
 Target Server Version : 3008008
 File Encoding         : utf-8

 Date: 01/18/2019 16:26:02 PM
*/

PRAGMA foreign_keys = false;

-- ----------------------------
--  Table structure for cat
-- ----------------------------
DROP TABLE IF EXISTS "cat";
CREATE TABLE "cat" (
	`id`	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
	`name`	TEXT NOT NULL UNIQUE,
	`depth`	INTEGER NOT NULL DEFAULT 1,
	`parentId`	INTEGER NOT NULL,
	`desc`	TEXT
);
INSERT INTO "main".sqlite_sequence (name, seq) VALUES ("cat", '45');

-- ----------------------------
--  Records of cat
-- ----------------------------
BEGIN;
INSERT INTO "cat" VALUES (1, 'element', 1, 0, '布局');
INSERT INTO "cat" VALUES (2, 'components', 1, 0, '组件');
INSERT INTO "cat" VALUES (3, 'themes', 1, 0, '主题');
INSERT INTO "cat" VALUES (4, 'form', 2, 1, 'Form表单');
INSERT INTO "cat" VALUES (5, 'Input', 3, 4, '输入');
INSERT INTO "cat" VALUES (6, 'Checkbox', 3, 4, null);
INSERT INTO "cat" VALUES (7, 'Button', 3, 4, '单选框');
INSERT INTO "cat" VALUES (8, 'Text', 3, 4, '文本');
INSERT INTO "cat" VALUES (9, 'Radio', 3, 4, '单选');
INSERT INTO "cat" VALUES (10, 'Slider', 3, 4, '范围选择器');
INSERT INTO "cat" VALUES (11, 'Switch', 3, 4, '按钮开关');
INSERT INTO "cat" VALUES (17, 'Frame', 2, 1, 'desc');
INSERT INTO "cat" VALUES (18, 'Media', 2, 1, 'Media(媒介)');
INSERT INTO "cat" VALUES (19, 'Align', 3, 17, 'Align');
INSERT INTO "cat" VALUES (20, 'Stack', 3, 17, 'Stack');
INSERT INTO "cat" VALUES (21, 'Layout', 3, 17, 'Layout(布局)');
INSERT INTO "cat" VALUES (22, 'Box', 3, 17, 'Box');
INSERT INTO "cat" VALUES (24, 'Expanded', 3, 17, 'Expanded');
INSERT INTO "cat" VALUES (25, 'spacing', 3, 17, 'spacing');
INSERT INTO "cat" VALUES (26, 'Table', 3, 17, 'Table');
INSERT INTO "cat" VALUES (27, 'Image', 3, 18, 'Image');
INSERT INTO "cat" VALUES (28, 'Icon', 3, 18, 'Icon');
INSERT INTO "cat" VALUES (29, 'Canvas', 3, 18, 'Canvas');
INSERT INTO "cat" VALUES (30, 'Navigation', 2, 2, 'Navigation');
INSERT INTO "cat" VALUES (31, 'List', 2, 2, 'List');
INSERT INTO "cat" VALUES (32, 'Card', 2, 2, 'Card');
INSERT INTO "cat" VALUES (33, 'Bar', 2, 2, 'Bar');
INSERT INTO "cat" VALUES (34, 'Dialog', 2, 2, 'Dialog');
INSERT INTO "cat" VALUES (35, 'Scaffold', 2, 2, 'Scaffold');
INSERT INTO "cat" VALUES (36, 'Grid', 2, 2, '网格布局');
INSERT INTO "cat" VALUES (37, 'Scroll', 2, 2, '滚动视图');
INSERT INTO "cat" VALUES (38, 'Tab', 2, 2, 'Tab');
INSERT INTO "cat" VALUES (39, 'Menu', 2, 2, 'Menu');
INSERT INTO "cat" VALUES (40, 'Pick', 2, 2, 'Pick');
INSERT INTO "cat" VALUES (41, 'Chip', 2, 2, 'Chip');
INSERT INTO "cat" VALUES (42, 'Panel', 2, 2, 'Panel');
INSERT INTO "cat" VALUES (43, 'Progress', 2, 2, 'Progress');
INSERT INTO "cat" VALUES (44, 'Material', 2, 3, 'Material');
INSERT INTO "cat" VALUES (45, 'Cupertino', 2, 3, 'Cupertino');
COMMIT;

-- ----------------------------
--  Table structure for collection
-- ----------------------------
DROP TABLE IF EXISTS "collection";
CREATE TABLE collection (id INTEGER PRIMARY KEY NOT NULL UNIQUE, name TEXT NOT NULL, router TEXT);

-- ----------------------------
--  Table structure for widget
-- ----------------------------
DROP TABLE IF EXISTS "widget";
CREATE TABLE widget (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, cnName TEXT NOT NULL, image TEXT NOT NULL, doc TEXT, demo TEXT, catId INTEGER NOT NULL REFERENCES cat (id), owner TEXT);
INSERT INTO "main".sqlite_sequence (name, seq) VALUES ("widget", '158');

-- ----------------------------
--  Records of widget
-- ----------------------------
BEGIN;
INSERT INTO "widget" VALUES (4, 'TextField', 'TextField', 'image', 'doc', 'demo', 5, '一晟');
INSERT INTO "widget" VALUES (5, 'CheckboxListTile', 'CheckboxListTile', '', 'doc', 'demo', 6, '一晟');
INSERT INTO "widget" VALUES (6, 'Checkbox', 'Checkbox', '', 'doc', 'demo', 6, '一晟');
INSERT INTO "widget" VALUES (7, 'FlatButton', 'FlatButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (8, 'RaisedButton', 'RaisedButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (9, 'IconButton', 'IconButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (10, 'PopupMenuButton', 'PopupMenuButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (11, 'FloatingActionButton', 'FloatingActionButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (12, 'RawMaterialButton', 'RawMaterialButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (13, 'DropdownButton', 'DropdownButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (14, 'Text', 'Text', '', 'doc', 'demo', 8, '三帆');
INSERT INTO "widget" VALUES (15, 'RichText', 'RichText', '', 'doc', 'demo', 8, '三帆');
INSERT INTO "widget" VALUES (16, 'Radio', 'Radio', '', 'doc', 'demo', 9, '三帆');
INSERT INTO "widget" VALUES (17, 'RadioListTile', 'RadioListTile', '', 'doc', 'demo', 9, '三帆');
INSERT INTO "widget" VALUES (18, 'Slider', 'Slider', '', 'doc', 'demo', 10, '三帆');
INSERT INTO "widget" VALUES (19, 'SliderTheme', 'SliderTheme', '', 'doc', 'demo', 10, '三帆');
INSERT INTO "widget" VALUES (21, 'SliderThemeData', 'SliderThemeData', '', 'doc', 'demo', 10, '三帆');
INSERT INTO "widget" VALUES (22, 'Switch', 'Switch', '', 'doc', 'demo', 11, '三帆');
INSERT INTO "widget" VALUES (23, 'SwitchListTile', 'SwitchListTile', '', 'doc', 'demo', 11, '三帆');
INSERT INTO "widget" VALUES (24, 'AnimatedSwitcher', 'AnimatedSwitcher', '', 'doc', 'demo', 11, '三帆');
INSERT INTO "widget" VALUES (25, 'Align', 'Align', '', 'doc', 'demo', 19, '三露');
INSERT INTO "widget" VALUES (26, 'Stack', 'Stack', '', 'doc', 'demo', 20, '三露');
INSERT INTO "widget" VALUES (27, 'IndexedStack', 'IndexedStack', '', 'doc', 'demo', 20, '三露');
INSERT INTO "widget" VALUES (28, 'Row', 'Row', '', 'doc', 'demo', 21, '三露');
INSERT INTO "widget" VALUES (29, 'Column', 'Column', '', 'doc', 'demo', 21, '三露');
INSERT INTO "widget" VALUES (30, 'Container', 'Container', '', 'doc', 'demo', 21, '三露');
INSERT INTO "widget" VALUES (31, 'Center', 'Center', '', 'doc', 'demo', 21, '三露');
INSERT INTO "widget" VALUES (32, 'ConstrainedBox', 'ConstrainedBox(带限制的盒子)', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (33, 'OverflowBox', 'OverflowBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (34, 'DecoratedBox', 'DecoratedBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (35, 'FittedBox', 'FittedBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (36, 'LimitedBox', 'LimitedBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (38, 'RotatedBox', 'RotatedBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (39, 'SizedOverflowBox', 'SizedOverflowBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (41, 'UnconstrainedBox', 'UnconstrainedBox', '', 'doc', 'demo', 22, '三露');
INSERT INTO "widget" VALUES (45, 'Expanded', 'Expanded', '', 'doc', 'demo', 24, '一凨');
INSERT INTO "widget" VALUES (46, 'Padding', 'Padding', '', 'doc', 'demo', 25, '一凨');
INSERT INTO "widget" VALUES (47, 'SliverPadding', 'SliverPadding', '', 'doc', 'demo', 25, '一凨');
INSERT INTO "widget" VALUES (48, 'AnimatedPadding', 'AnimatedPadding', '', 'doc', 'demo', 25, '一凨');
INSERT INTO "widget" VALUES (49, 'Table', 'Table', '', 'doc', 'demo', 26, '一凨');
INSERT INTO "widget" VALUES (52, 'AssetImage', 'AssetImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (53, 'DecorationImage', 'DecorationImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (55, 'ExactAssetImage', 'ExactAssetImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (56, 'FadeInImage', 'FadeInImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (57, 'FileImage', 'FileImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (58, 'NetworkImage', 'NetworkImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (59, 'RawImage', 'RawImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (62, 'MemoryImage', 'MemoryImage', '', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (63, 'Icon', 'Icon', '', 'doc', 'demo', 28, '剑平');
INSERT INTO "widget" VALUES (64, 'ImageIcon', 'ImageIcon', '', 'doc', 'demo', 28, '剑平');
INSERT INTO "widget" VALUES (65, 'IconTheme', 'IconTheme', '', 'doc', 'demo', 28, '剑平');
INSERT INTO "widget" VALUES (66, 'IconData', 'IconData', '', 'doc', 'demo', 28, '剑平');
INSERT INTO "widget" VALUES (67, 'IconThemeData', 'IconThemeData', '', 'doc', 'demo', 28, '剑平');
INSERT INTO "widget" VALUES (69, 'Canvas', 'Canvas', '', 'doc', 'demo', 29, '剑平');
INSERT INTO "widget" VALUES (70, 'OutlineButton', 'OutlineButton', '', 'doc', 'demo', 7, '一晟');
INSERT INTO "widget" VALUES (71, 'BottomNavigationBarItem', 'BottomNavigationBarItem', 'image', 'doc', 'demo', 30, '一晟');
INSERT INTO "widget" VALUES (73, 'BottomNavigationBar', 'BottomNavigationBar', 'image', 'doc', 'demo', 30, '一晟');
INSERT INTO "widget" VALUES (74, 'AnimatedList', 'AnimatedList', 'image', 'doc', 'demo', 31, '一晟');
INSERT INTO "widget" VALUES (75, 'ListView', 'ListView', 'image', 'doc', 'demo', 31, '一晟');
INSERT INTO "widget" VALUES (78, 'Card', 'Card', 'image', 'doc', 'demo', 32, '一晟');
INSERT INTO "widget" VALUES (79, 'AppBar', 'AppBar', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (80, 'BottomAppBar', 'BottomAppBar', 'image', 'dec', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (81, 'BottomNavigationBarItem', 'BottomNavigationBarItem', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (82, 'SnackBar', 'SnackBar', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (83, 'SliverAppBar', 'SliverAppBar', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (84, 'ScrollbarPainter', 'ScrollbarPainter', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (87, 'FlexibleSpaceBar', 'FlexibleSpaceBar', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (88, 'ButtonBar', 'ButtonBar', 'image', 'doc', 'demo', 33, '一晟');
INSERT INTO "widget" VALUES (89, 'SnackBarAction', 'SnackBarAction', 'image', 'doc', 'demo', 33, '三帆');
INSERT INTO "widget" VALUES (90, 'TabBar', 'TabBar', 'image', 'doc', 'demo', 33, '三帆');
INSERT INTO "widget" VALUES (93, 'Scaffold', 'Scaffold', 'image', 'doc', 'demo', 35, '三帆');
INSERT INTO "widget" VALUES (97, 'ScaffoldState', 'ScaffoldState', 'image', 'doc', 'demo', 35, '三帆');
INSERT INTO "widget" VALUES (98, 'GridTile', 'GridTile', 'image', 'doc', 'demo', 36, '三帆');
INSERT INTO "widget" VALUES (99, 'GridView', 'GridView', 'image', 'doc', 'demo', 36, '三帆');
INSERT INTO "widget" VALUES (100, 'GridPaper', 'GridPaper', 'image', 'doc', 'demo', 36, '三帆');
INSERT INTO "widget" VALUES (101, 'SliverGrid', 'SliverGrid', 'image', 'doc', 'demo', 36, '三帆');
INSERT INTO "widget" VALUES (102, 'SliverGridDelegate', 'SliverGridDelegate', 'image', 'doc', 'demo', 36, '三帆');
INSERT INTO "widget" VALUES (103, 'AlertDialog', 'AlertDialog', 'image', 'doc', 'demo', 34, '三帆');
INSERT INTO "widget" VALUES (104, 'Dialog', 'Dialog', 'image', 'doc', 'demo', 34, '三帆');
INSERT INTO "widget" VALUES (105, 'AboutDialog', 'AboutDialog', 'image', 'doc', 'demo', 34, '三帆');
INSERT INTO "widget" VALUES (106, 'SimpleDialog', 'SimpleDialog', 'image', 'doc', 'demo', 34, '三帆');
INSERT INTO "widget" VALUES (108, 'ScrollView', 'ScrollView', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (109, 'Scrollable', 'Scrollable', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (110, 'ScrollbarPainter', 'ScrollbarPainter', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (111, 'ScrollMetrics', 'ScrollMetrics', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (112, 'ScrollPhysics', 'ScrollPhysics', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (113, 'BoxScrollView', 'BoxScrollView', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (116, 'CustomScrollView', 'CustomScrollView', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (118, 'NestedScrollView', 'NestedScrollView', 'image', 'doc', 'demo', 37, '一凨');
INSERT INTO "widget" VALUES (120, 'Tab', 'Tab', 'image', 'doc', 'demo', 38, '一凨');
INSERT INTO "widget" VALUES (121, 'CheckedPopupMenuItem', 'CheckedPopupMenuItem', 'image', 'doc', 'demo', 39, '一凨');
INSERT INTO "widget" VALUES (122, 'DropdownMenuItem', 'DropdownMenuItem', 'image', 'doc', 'demo', 39, '一凨');
INSERT INTO "widget" VALUES (123, 'PopupMenuButton', 'PopupMenuButton', 'image', 'doc', 'demo', 39, '一凨');
INSERT INTO "widget" VALUES (124, 'PopupMenuDivider', 'PopupMenuDivider', 'image', 'doc', 'demo', 39, '一凨');
INSERT INTO "widget" VALUES (125, 'PopupMenuEntry', 'PopupMenuEntry', 'image', 'doc', 'demo', 39, '一凨');
INSERT INTO "widget" VALUES (126, 'PopupMenuItem', 'PopupMenuItem', 'image', 'doc', 'demo', 39, '三露');
INSERT INTO "widget" VALUES (128, 'DayPicker', 'DayPicker', 'image', 'doc', 'demo', 40, '三露');
INSERT INTO "widget" VALUES (129, 'MonthPicker', 'MonthPicker', 'image', 'doc', 'demo', 40, '三露');
INSERT INTO "widget" VALUES (130, 'YearPicker', 'YearPicker', 'image', 'doc', 'demo', 40, '三露');
INSERT INTO "widget" VALUES (131, 'Chip', 'Chip', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (132, 'ChipTheme', 'ChipTheme', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (133, 'ChipThemeData', 'ChipThemeData', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (134, 'ChoiceChip', 'ChoiceChip', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (135, 'FilterChip', 'FilterChip', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (136, 'InputChip', 'InputChip', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (137, 'RawChip', 'RawChip', 'image', 'doc', 'demo', 41, '三露');
INSERT INTO "widget" VALUES (139, 'ExpansionPanelList', 'ExpansionPanelList', 'image', 'doc', 'demo', 42, '三露');
INSERT INTO "widget" VALUES (141, 'LinearProgressIndicator', 'LinearProgressIndicator', 'image', 'doc', 'demo', 43, '三露');
INSERT INTO "widget" VALUES (142, 'CircularProgressIndicator', 'CircularProgressIndicator', 'image', 'doc', 'demo', 43, '三露');
INSERT INTO "widget" VALUES (146, 'MaterialApp', 'MaterialApp', 'image', 'doc', 'demo', 44, '一凨');
INSERT INTO "widget" VALUES (149, 'MaterialColor', 'MaterialColor', 'image', 'doc', 'demo', 44, '一凨');
INSERT INTO "widget" VALUES (150, 'MaterialButton', 'MaterialButton', 'image', 'doc', 'demo', 44, '一凨');
INSERT INTO "widget" VALUES (151, 'MaterialPageRoute', 'MaterialPageRoute', 'image', 'doc', 'demo', 44, '三露');
INSERT INTO "widget" VALUES (152, 'MaterialAccentColor', 'MaterialAccentColor', 'image', 'doc', 'demo', 44, '三露');
INSERT INTO "widget" VALUES (155, 'MergeableMaterialItem', 'MergeableMaterialItem', 'image', 'doc', 'demo', 44, '三露');
INSERT INTO "widget" VALUES (156, 'ShowdatePicker', 'ShowdatePicker', 'image', 'doc', 'demo', 40, '三露');
INSERT INTO "widget" VALUES (157, 'Image', 'Image', 'Image', 'doc', 'demo', 27, '一凨');
INSERT INTO "widget" VALUES (158, 'ListBody', 'ListBody', 'image', 'doc', 'demo', 31, '一晟');
COMMIT;

PRAGMA foreign_keys = true;

主要就是建表,插值。
因为我使用的AndroidStudio来运行FlutterGo的,所以我就决定使用groovy脚本来生成.sql脚本,代码如下:


final def dbName = "app.db"
final def sqlName = "main.sql"
final def TARGET_DB_DIR = '../db/'
final def WIDGET_DIR = '../lib/widgets/'

gradle.projectsEvaluated {
    assembleDebug.doLast {
        def dir = new File(TARGET_DB_DIR)
        if (!dir.exists()) {
            dir.mkdirs()
        }
        def dbFile = new File(TARGET_DB_DIR + dbName)
        try {
            if (!dbFile.exists()) {
                dbFile.createNewFile()
            }
        } catch (IOException e) {
            e.printStackTrace()
        }
        def file = new File(TARGET_DB_DIR + sqlName)
        if (file.exists()) {
            file.delete()
        }
        def printWriter = file.newPrintWriter()
        printWriter.write('DROP TABLE IF EXISTS "collection";n')
        printWriter.write('CREATE TABLE collection (id INTEGER NOT NULL PRIMARY KEY UNIQUE, name TEXT NOT NULL, router TEXT);n')
        printWriter.write('DROP TABLE IF EXISTS "widget";n')
        printWriter.write('CREATE TABLE widget (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, cnName TEXT, image TEXT, doc TEXT, catId INTEGER NOT NULL REFERENCES cat (id), owner TEXT);n')
        printWriter.write('DROP TABLE IF EXISTS "cat";n')
        printWriter.write('CREATE TABLE cat (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, depth INTEGER NOT NULL DEFAULT 1, parentId INTEGER NOT NULL, desc TEXT);n')
        printWriter.write('BEGIN;n')
        def widgetDir = new File(WIDGET_DIR)
        def catId = 0
        def widgetId = 0
        def calculatDepth
        calculatDepth = { File widgetFile, int depth, int parentId ->
            if (widgetFile.isDirectory()) {
                widgetFile.eachFile { widget ->
                    if (widget.isDirectory()) {
                        def hasDemo = false
                        def onlyHaveIndex = true
                        widget.eachFile { widget2 ->
                            if ("demo.dart" == widget2.name) {
                                hasDemo = true
                            }
                            if (widget2.isDirectory()) {
                                onlyHaveIndex = false
                            }
                        }
                        if (hasDemo || onlyHaveIndex) {
                            calculatDepth(widget, depth + 1, catId)
                        } else {
                            catId++
                            printWriter.write('INSERT INTO "cat" ("name","depth","parentId") VALUES ("' + widget.name + '",' + depth + ',' + parentId + ');n')
//                            println 'catId:' + catId + widget.name + '  depth:' + depth + '   parentId:' + parentId
                            calculatDepth(widget, depth + 1, catId)
                        }
                    } else {
                        if ("demo.dart" == widget.name) {
                            widgetId++
                            printWriter.write('INSERT INTO "widget" ("name","catId") VALUES ("' + widgetFile.name + '",' + catId + ');n')
//                            println 'widgetId:' + widgetId + widgetFile.name + ' catId:' + catId
                        }
                    }
                }
            }
        }
        calculatDepth(widgetDir, 1, 0)
        printWriter.write('COMMIT;n')
        printWriter.flush()
        printWriter.close()
    }
}

在flutter-go-master工程目录下新建db文件夹,并新建空的app.db文件和main.sql文件;
sql语句的意思:先删除原表并新建表,为防止删除原表失败先删除外键表并新建之,然后是插入语句;
为遍历文件夹,写了一个闭包递归,如果是"demo.dart"文件则插入widget表中并外键关联cat表,如果是文件夹且还有子文件夹则加入到cat表。
cat表与widget表具体含义:

abstract class CatInterface{
    int get id;
    //类目名称
    String get name;
    //描述
    String get desc;
    //第几级类目,默认 1
    int get depth;
    //父类目id,没有为 0
    int get parentId;
}
abstract class WidgetInterface {
  int get id;
  //组件英文名
  String get name;
  //组件中文名
  String get cnName;
  //组件截图
  String get image;
  //组件markdown 文档
  String get doc;
  //类目 id
  int get catId;
}

最终生成的sql语句如下:

DROP TABLE IF EXISTS "collection";
CREATE TABLE collection (id INTEGER NOT NULL PRIMARY KEY UNIQUE, name TEXT NOT NULL, router TEXT);
DROP TABLE IF EXISTS "widget";
CREATE TABLE widget (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, cnName TEXT, image TEXT, doc TEXT, catId INTEGER NOT NULL REFERENCES cat (id), owner TEXT);
DROP TABLE IF EXISTS "cat";
CREATE TABLE cat (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, depth INTEGER NOT NULL DEFAULT 1, parentId INTEGER NOT NULL, desc TEXT);
BEGIN;
INSERT INTO "cat" ("name","depth","parentId") VALUES ("components",1,0);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Card",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("Card",2);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Scroll",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("CustomScrollView",3);
INSERT INTO "widget" ("name","catId") VALUES ("ScrollMetrics",3);
INSERT INTO "widget" ("name","catId") VALUES ("BoxScrollView",3);
INSERT INTO "widget" ("name","catId") VALUES ("ScrollView",3);
INSERT INTO "widget" ("name","catId") VALUES ("NestedScrollView",3);
INSERT INTO "widget" ("name","catId") VALUES ("ScrollbarPainter",3);
INSERT INTO "widget" ("name","catId") VALUES ("Scrollable",3);
INSERT INTO "widget" ("name","catId") VALUES ("ScrollPhysics",3);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Panel",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("ExpansionPanel",4);
INSERT INTO "widget" ("name","catId") VALUES ("ExpansionPanelList",4);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Progress",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("CircularProgressIndicator",5);
INSERT INTO "widget" ("name","catId") VALUES ("LinearProgressIndicator",5);
INSERT INTO "widget" ("name","catId") VALUES ("RefreshProgressIndicator",5);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Chip",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("ChipTheme",6);
INSERT INTO "widget" ("name","catId") VALUES ("RawChip",6);
INSERT INTO "widget" ("name","catId") VALUES ("Chip",6);
INSERT INTO "widget" ("name","catId") VALUES ("ChoiceChip",6);
INSERT INTO "widget" ("name","catId") VALUES ("FilterChip",6);
INSERT INTO "widget" ("name","catId") VALUES ("ChipThemeData",6);
INSERT INTO "widget" ("name","catId") VALUES ("inputChip",6);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Navigation",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("BottomNavigationBar",7);
INSERT INTO "widget" ("name","catId") VALUES ("BottomNavigationBarItem",7);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Tab",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("Tab",8);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Dialog",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("SimpleDialog",9);
INSERT INTO "widget" ("name","catId") VALUES ("Dialog",9);
INSERT INTO "widget" ("name","catId") VALUES ("AboutDialog",9);
INSERT INTO "widget" ("name","catId") VALUES ("AlertDialog",9);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("LIst",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("ListBody",10);
INSERT INTO "widget" ("name","catId") VALUES ("ListView",10);
INSERT INTO "widget" ("name","catId") VALUES ("AnimatedList",10);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Menu",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("DropdownMenuItem",11);
INSERT INTO "widget" ("name","catId") VALUES ("CheckedPopupMenuItem",11);
INSERT INTO "widget" ("name","catId") VALUES ("PopupMenuDivider",11);
INSERT INTO "widget" ("name","catId") VALUES ("PopupMenuButton",11);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Bar",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("AppBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("ButtonBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("SliverAppBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("SnackBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("BottomAppBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("TabBar",12);
INSERT INTO "widget" ("name","catId") VALUES ("SnackBarAction",12);
INSERT INTO "widget" ("name","catId") VALUES ("FlexibleSpaceBar",12);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Scaffold",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("ScaffoldState",13);
INSERT INTO "widget" ("name","catId") VALUES ("Scaffold",13);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Pick",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("DayPicker",14);
INSERT INTO "widget" ("name","catId") VALUES ("ShowdatePicker",14);
INSERT INTO "widget" ("name","catId") VALUES ("YearPicker",14);
INSERT INTO "widget" ("name","catId") VALUES ("MonthPicker",14);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Grid",2,1);
INSERT INTO "widget" ("name","catId") VALUES ("SliverGrid",15);
INSERT INTO "widget" ("name","catId") VALUES ("GridTileBar",15);
INSERT INTO "widget" ("name","catId") VALUES ("GridTile",15);
INSERT INTO "widget" ("name","catId") VALUES ("GridPaper",15);
INSERT INTO "widget" ("name","catId") VALUES ("GridView",15);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("elements",1,0);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Form",2,16);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Radio",3,17);
INSERT INTO "widget" ("name","catId") VALUES ("RadioListTile",18);
INSERT INTO "widget" ("name","catId") VALUES ("Radio",18);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Input",3,17);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("CheckBox",3,17);
INSERT INTO "widget" ("name","catId") VALUES ("Checkbox",20);
INSERT INTO "widget" ("name","catId") VALUES ("CheckboxListTile",20);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Slider",3,17);
INSERT INTO "widget" ("name","catId") VALUES ("SliderTheme",21);
INSERT INTO "widget" ("name","catId") VALUES ("SliderThemeData",21);
INSERT INTO "widget" ("name","catId") VALUES ("Slider",21);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Button",3,17);
INSERT INTO "widget" ("name","catId") VALUES ("FloatingActionButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("OutlineButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("RawMaterialButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("RaisedButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("IconButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("FlatButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("DropdownButton",22);
INSERT INTO "widget" ("name","catId") VALUES ("PopupMenuButton",22);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Switch",3,17);
INSERT INTO "widget" ("name","catId") VALUES ("AnimatedSwitcher",23);
INSERT INTO "widget" ("name","catId") VALUES ("Switch",23);
INSERT INTO "widget" ("name","catId") VALUES ("SwitchListTile",23);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Text",3,17);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Frame",2,16);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Expanded",3,25);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Box",3,25);
INSERT INTO "widget" ("name","catId") VALUES ("RotatedBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("SizeBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("OverflowBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("RenderBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("DecoratedBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("UnconstrainedBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("ConstrainedBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("Fittedbox",27);
INSERT INTO "widget" ("name","catId") VALUES ("TextBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("LimitedBox",27);
INSERT INTO "widget" ("name","catId") VALUES ("SizedOverflowBox",27);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Layout",3,25);
INSERT INTO "widget" ("name","catId") VALUES ("Center",28);
INSERT INTO "widget" ("name","catId") VALUES ("Column",28);
INSERT INTO "widget" ("name","catId") VALUES ("Container",28);
INSERT INTO "widget" ("name","catId") VALUES ("Row",28);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Align",3,25);
INSERT INTO "widget" ("name","catId") VALUES ("Align",29);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Table",3,25);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Spacing",3,25);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Stack",3,25);
INSERT INTO "widget" ("name","catId") VALUES ("IndexedStack",32);
INSERT INTO "widget" ("name","catId") VALUES ("Stack",32);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Media",2,16);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Image",3,33);
INSERT INTO "widget" ("name","catId") VALUES ("Image",34);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Icon",3,33);
INSERT INTO "widget" ("name","catId") VALUES ("IconThemeData",35);
INSERT INTO "widget" ("name","catId") VALUES ("ImageIcon",35);
INSERT INTO "widget" ("name","catId") VALUES ("IconData",35);
INSERT INTO "widget" ("name","catId") VALUES ("Icon",35);
INSERT INTO "widget" ("name","catId") VALUES ("IconTheme",35);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("themes",1,0);
INSERT INTO "cat" ("name","depth","parentId") VALUES ("Material",2,36);
INSERT INTO "widget" ("name","catId") VALUES ("MaterialApp",37);
INSERT INTO "widget" ("name","catId") VALUES ("MaterialButton",37);
INSERT INTO "widget" ("name","catId") VALUES ("MaterialColor",37);
INSERT INTO "widget" ("name","catId") VALUES ("MergeableMaterialItem",37);
INSERT INTO "widget" ("name","catId") VALUES ("MaterialPageRoute",37);
COMMIT;

使用NavicatPremium打开db文件夹下的app.db文件,并执行main.sql文件,生成的数据表结构如下:
         
使用db文件夹下的app.db文件替换asset文件夹下的原文件,再次运行程序,最终效果:
         
收藏两个widget后,导出flutter.db文件,可以看到collection表的变化
      
注:app.db替换后有时还是展示原来的app.db中的数据,不管是重启AndroidStudio还是卸载APP甚至wipe data,有时他还是展示原文件的数据
项目中初始化数据库的代码如下,为使用sqlite使用了sqflite: ^0.12.1三方库https://pub.dartlang.org/packages/sqflite

  Future init(bool isCreate) async {
    //Get a location using getDatabasesPath
    String databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'flutter.db');

    try {
      db = await openDatabase(path);
    } catch (e) {
      print("Error $e");
    }
    bool tableIsRight = await this.checkTableIsRight();

    if (!tableIsRight) {
      // 关闭上面打开的db,否则无法执行open
      db.close();
      // Delete the database
      await deleteDatabase(path);
      ByteData data = await rootBundle.load(join("assets", "app.db"));
      List<int> bytes =
          data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
      await new File(path).writeAsBytes(bytes);

      db = await openDatabase(path, version: 1,
          onCreate: (Database db, int version) async {
        print('db created version is $version');
      }, onOpen: (Database db) async {
        print('new db opened');
      });
    } else {
      print("Opening existing database");
    }
  }

可直接用AndroidStudio运行,并已在build.gradle中写完脚本的项目地址https://gitee.com/xydzjnq/flutter-go-master
原项目地址https://github.com/alibaba/flutter-go?utm_source=androidweekly.io&utm_medium=website

 

最后

以上就是整齐奇异果为你收集整理的Groovy读取文件夹目录结构,并生成.sql文件的全部内容,希望文章能够帮你解决Groovy读取文件夹目录结构,并生成.sql文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部