flr 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e7b9ee5330233ba9aac3017cd51d403139365b6f95bd0729e188671dfadfb35
4
- data.tar.gz: 8be015c230dc9581326c39f7b5dd53074779e7f6291c911a18f9e9afac20ef7c
3
+ metadata.gz: 0b4e690c98b54e5cb00ed7136585e7d86d47b14a499fff89b8c607faf750cfb7
4
+ data.tar.gz: 5a7335ddb9a37e4d94aee57ce86d291ea971d9ead763663f91c355210b07e0a2
5
5
  SHA512:
6
- metadata.gz: 3ae44108a8e59cd1680700276f5b1d47a6235c5624566c6195f17bd522d901b02182faecee8dba056c200723f57f54e379ca37a8483c0493dc6dfe5a3678e7e2
7
- data.tar.gz: 98b5bf07ab3bce3305572555d58ef71037d9c6822aa1f4842fcb1d07e8090bf9a198b40cd269e08bbdd18103c4419d6b2b30f38b8cff6ce7300c9c2c613e937d
6
+ metadata.gz: 14dd754b4e3277fc773128058f8a167501c519e056aeb18e8e7f4ae0b1eb351d88da61b0482509ef2fbe3bd378d24cbaf09f39b52d8ae8f1412b92deb25e819a
7
+ data.tar.gz: 9ba05b656a9ed2c2e1c95004f1f4a0ce2e2fa1b43a0650164c3de5415f886376cb0a8bd3a3cfbda66ec5b7eccb97a960fb0fa958ee0194f06a12d71d4d683c66
@@ -1,3 +1,11 @@
1
+ ## 3.1.0
2
+
3
+ - Support for processing(init/generate/monitor) multi projects(the main project and its sub projects in one workspace)
4
+
5
+ - Support for auto merging old asset specifications when specifying new assets
6
+
7
+ > This is can help you to auto keep the manually added asset specifications.
8
+
1
9
  ## 3.0.0
2
10
 
3
11
  - Support for processing non-implied resource file
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flr (3.0.0)
4
+ flr (3.1.0)
5
5
  bundler (~> 2.0, >= 2.0.2)
6
6
  listen (~> 3.0, >= 3.2.1)
7
7
  thor (~> 1.0, >= 1.0.1)
data/lib/flr.rb CHANGED
@@ -42,7 +42,7 @@ More details see https://github.com/Fly-Mix/flr-cli
42
42
 
43
43
  LONGDESC
44
44
  def init
45
- Command.init
45
+ Command.init_all
46
46
  end
47
47
 
48
48
  desc "run [--auto]", "Scan assets, specify scanned assets in pubspec.yaml, generate \"r.g.dart\""
@@ -62,7 +62,7 @@ More details see https://github.com/Fly-Mix/flr-cli
62
62
  LONGDESC
63
63
  option :auto, :type => :boolean
64
64
  def run_command
65
- options[:auto] ? Command.start_monitor : Command.generate
65
+ options[:auto] ? Command.start_monitor : Command.generate_all
66
66
  end
67
67
  map 'run' => :run_command
68
68
 
@@ -164,31 +164,86 @@ Flr recommends the following flutter resource structure schemes:
164
164
  return r_dart_library_version
165
165
  end
166
166
 
167
- # 对 flutter 工程进行初始化
168
- def self.init
169
- flutter_project_root_dir = FileUtil.get_cur_flutter_project_root_dir
167
+ def self.init_all
168
+ flutter_main_project_root_dir = FileUtil.get_flutter_main_project_root_dir
170
169
 
171
170
  # ----- Step-1 Begin -----
172
- # 进行环境检测:
173
- # - 检测当前 flutter 工程根目录是否存在 pubspec.yaml
171
+ # 进行环境检测;若发现不合法的环境,则抛出异常,终止当前进程
172
+ # - 检测当前flutter主工程根目录是否存在 pubspec.yaml
174
173
  #
175
174
 
176
175
  begin
177
- Checker.check_pubspec_file_is_existed(flutter_project_root_dir)
176
+ Checker.check_pubspec_file_is_existed(flutter_main_project_root_dir)
177
+ rescue Exception => e
178
+ puts(e.message)
179
+ return
180
+ end
178
181
 
179
- pubspec_file_path = FileUtil.get_pubspec_file_path(flutter_project_root_dir)
182
+ # ----- Step-1 End -----
180
183
 
181
- pubspec_config = FileUtil.load_pubspec_config_from_file(pubspec_file_path)
184
+ puts("init all flutter projects now...")
185
+
186
+ # ----- Step-2 Begin -----
187
+ # 获取主工程和其所有子工程,对它们进行init_one操作
188
+ # - 获取flutter主工程根目录下所有的子工程目录
189
+ # - 初始化主工程
190
+ # - 初始化所有子工程
191
+ #
192
+ flutter_sub_project_root_dir_array = FileUtil.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir)
193
+
194
+ puts("")
195
+ init_one(flutter_main_project_root_dir)
196
+
197
+ flutter_sub_project_root_dir_array.each do |flutter_project_root_dir|
198
+ puts("")
199
+ init_one(flutter_project_root_dir)
200
+ end
201
+
202
+ # ----- Step-2 End -----
182
203
 
204
+ # ----- Step-3 Begin -----
205
+ # 调用 flutter 工具,为主工程和所有子工程获取依赖
206
+ #
207
+ puts("")
208
+ puts("get dependencies for all flutter projects via execute \"flutter pub get\" now ...")
209
+
210
+ get_flutter_pub_cmd = "flutter pub get"
211
+ system(get_flutter_pub_cmd)
212
+
213
+ puts("[√]: get dependencies for all flutter projects done !!!")
214
+
215
+ # ----- Step-3 End -----
216
+ puts("")
217
+ puts("[√]: init all flutter projects done !!!")
218
+
219
+ puts("")
220
+ puts("[*]: if you want to know how to make a good resource structure for your flutter project, please run \"flr recommend\" ".tips_style)
221
+ end
222
+
223
+ # 对指定 flutter 工程进行初始化
224
+ def self.init_one(flutter_project_root_dir)
225
+ puts("------------------------------- init specified project -------------------------------")
226
+
227
+ puts("init #{flutter_project_root_dir} now...")
228
+
229
+ # ----- Step-1 Begin -----
230
+ # 进行环境检测;若发现不合法的环境,则抛出异常,终止当前进程
231
+ # - 检测 pubspec.yaml 是否存在
232
+ # - 检测 pubspec.yaml 是否合法
233
+ #
234
+ begin
235
+ Checker.check_pubspec_file_is_existed(flutter_project_root_dir)
236
+ pubspec_file_path = FileUtil.get_pubspec_file_path(flutter_project_root_dir)
237
+ pubspec_config = FileUtil.load_pubspec_config_from_file(pubspec_file_path)
183
238
  rescue Exception => e
184
239
  puts(e.message)
240
+ puts("[x]: init #{flutter_project_root_dir} failed".error_style)
241
+ puts("--------------------------------------------------------------------------------------")
185
242
  return
186
243
  end
187
244
 
188
245
  # ----- Step-1 End -----
189
246
 
190
- puts("init #{flutter_project_root_dir} now...")
191
-
192
247
  # ----- Step-2 Begin -----
193
248
  # 添加 flr_config 和 r_dart_library 的依赖声明到 pubspec.yaml
194
249
  #
@@ -264,6 +319,9 @@ Flr recommends the following flutter resource structure schemes:
264
319
  #
265
320
 
266
321
  flutter_config = pubspec_config["flutter"]
322
+ if flutter_config.nil?
323
+ flutter_config = {}
324
+ end
267
325
 
268
326
  flutter_assets = flutter_config["assets"]
269
327
  should_rm_flutter_assets_Key = true
@@ -290,29 +348,67 @@ Flr recommends the following flutter resource structure schemes:
290
348
  # 保存 pubspec.yaml
291
349
  FileUtil.dump_pubspec_config_to_file(pubspec_config, pubspec_file_path)
292
350
 
293
- puts("get dependency \"r_dart_library\" via execute \"flutter pub get\" now ...")
351
+ puts("[√]: init #{flutter_project_root_dir} done !!!")
352
+ puts("--------------------------------------------------------------------------------------")
294
353
 
295
- # ----- Step-4 Begin -----
296
- # 调用 flutter 工具,为 flutter 工程获取依赖
354
+ end
297
355
 
298
- get_flutter_pub_cmd = "flutter pub get"
299
- system(get_flutter_pub_cmd)
356
+ def self.generate_all
357
+ flutter_main_project_root_dir = FileUtil.get_flutter_main_project_root_dir
300
358
 
301
- puts("get dependency \"r_dart_library\" done !!!")
359
+ # ----- Step-1 Begin -----
360
+ # 进行环境检测;若发现不合法的环境,则抛出异常,终止当前进程。
361
+ # - 检测当前flutter主工程根目录是否存在 pubspec.yaml
362
+ #
363
+ begin
364
+ Checker.check_pubspec_file_is_existed(flutter_main_project_root_dir)
365
+ rescue Exception => e
366
+ puts(e.message)
367
+ return
368
+ end
302
369
 
303
- # ----- Step-4 End -----
370
+ # ----- Step-1 End -----
304
371
 
305
- puts("[√]: init done !!!")
372
+ puts("generate for all flutter projects now...")
373
+
374
+ # ----- Step-2 Begin -----
375
+ # 获取主工程和其所有子工程,对它们进行generate_one操作
376
+ # - 获取flutter主工程根目录下所有的子工程目录
377
+ # - 对主工程执行generate_one操作
378
+ # - 对所有子工程执行generate_one操作
379
+ #
380
+ flutter_sub_project_root_dir_array = FileUtil.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir)
306
381
 
307
382
  puts("")
308
- puts("[*]: if you want to know how to make a good resource structure for your flutter project, please run \"flr recommend\" ".tips_style)
383
+ generate_one(flutter_main_project_root_dir)
309
384
 
310
- end
385
+ flutter_sub_project_root_dir_array.each do |flutter_project_root_dir|
386
+ puts("")
387
+ generate_one(flutter_project_root_dir)
388
+ end
389
+
390
+ # ----- Step-2 End -----
311
391
 
312
- # 扫描资源目录,自动为资源添加声明到 pubspec.yaml 和生成 r.g.dart
313
- def self.generate
392
+ # ----- Step-3 Begin -----
393
+ # 调用 flutter 工具,为主工程和所有子工程获取依赖
394
+ #
395
+ puts("")
396
+ puts("get dependencies for all flutter projects via execute \"flutter pub get\" now ...")
397
+
398
+ get_flutter_pub_cmd = "flutter pub get"
399
+ system(get_flutter_pub_cmd)
400
+
401
+ puts("[√]: get dependencies for all flutter projects done !!!")
402
+
403
+ # ----- Step-3 End -----
404
+ puts("")
405
+ puts("[√]: generate for all flutter projects done !!!")
406
+ end
314
407
 
315
- flutter_project_root_dir = FileUtil.get_cur_flutter_project_root_dir
408
+ # 为指定 flutter 工程扫描资源目录,自动为资源添加声明到 pubspec.yaml 和生成 r.g.dart
409
+ def self.generate_one(flutter_project_root_dir)
410
+ puts("--------------------------- generate for specified project ---------------------------")
411
+ puts("generate for #{flutter_project_root_dir} now...")
316
412
 
317
413
  # 警告日志数组
318
414
  warning_messages = []
@@ -340,6 +436,8 @@ Flr recommends the following flutter resource structure schemes:
340
436
 
341
437
  rescue Exception => e
342
438
  puts(e.message)
439
+ puts("[x]: generate for #{flutter_project_root_dir} failed".error_style)
440
+ puts("--------------------------------------------------------------------------------------")
343
441
  return
344
442
  end
345
443
 
@@ -552,23 +650,36 @@ Flr recommends the following flutter resource structure schemes:
552
650
 
553
651
  # ----- Step-8 Begin -----
554
652
  # 为扫描得到的legal_resource_file添加资源声明到pubspec.yaml:
555
- # - 合并image_asset数组和text_asset数组为asset数组(image_asset数组元素在前);
653
+ # - 合并image_asset数组和text_asset数组为new_asset_array(image_asset数组元素在前);
654
+ # - 读取pubspec.yaml中flutter-assets配置,获得old_asset_array,然后和new_asset_array合并为asset数组;
556
655
  # - 修改pubspec.yaml中flutter-assets配置的值为asset数组;
557
656
  # - 修改pubspec.yaml中flutter-fonts配置的值为font_family_config数组。
558
657
 
559
- asset_array = image_asset_array + text_asset_array
658
+ flutter_config = pubspec_config["flutter"]
659
+ if flutter_config.nil?
660
+ flutter_config = {}
661
+ end
662
+
663
+ new_asset_array = image_asset_array + text_asset_array
664
+ old_asset_array = flutter_config["assets"]
665
+ if old_asset_array.nil? or old_asset_array.is_a?(Array) == false
666
+ old_asset_array = []
667
+ end
668
+
669
+ asset_array = AssetUtil.mergeFlutterAssets(flutter_project_root_dir, package_name, new_asset_array, old_asset_array)
560
670
  if asset_array.length > 0
561
- pubspec_config["flutter"]["assets"] = asset_array
671
+ flutter_config["assets"] = asset_array
562
672
  else
563
- pubspec_config["flutter"].delete("assets")
673
+ flutter_config.delete("assets")
564
674
  end
565
675
 
566
676
  if font_family_config_array.length > 0
567
- pubspec_config["flutter"]["fonts"] = font_family_config_array
677
+ flutter_config["fonts"] = font_family_config_array
568
678
  else
569
- pubspec_config["flutter"].delete("fonts")
679
+ flutter_config.delete("fonts")
570
680
  end
571
681
 
682
+ pubspec_config["flutter"] = flutter_config
572
683
  FileUtil.dump_pubspec_config_to_file(pubspec_config, pubspec_file_path)
573
684
 
574
685
  # ----- Step-8 End -----
@@ -734,19 +845,6 @@ Flr recommends the following flutter resource structure schemes:
734
845
  # ----- Step-21 End -----
735
846
 
736
847
  # ----- Step-22 Begin -----
737
- # 调用flutter工具,为flutter工程获取依赖
738
- #
739
-
740
- get_flutter_pub_cmd = "flutter pub get"
741
- puts("execute \"#{get_flutter_pub_cmd}\" now ...")
742
- system(get_flutter_pub_cmd)
743
- puts("execute \"#{get_flutter_pub_cmd}\" done !!!")
744
-
745
- # ----- Step-22 End -----
746
-
747
- puts("[√]: generate done !!!")
748
-
749
- # ----- Step-23 Begin -----
750
848
  # 判断警告日志数组是否为空,若不为空,输出所有警告日志
751
849
  #
752
850
 
@@ -757,87 +855,125 @@ Flr recommends the following flutter resource structure schemes:
757
855
  end
758
856
  end
759
857
 
760
- # ----- Step-23 End -----
858
+ # ----- Step-22 End -----
859
+
860
+ puts("[√]: generate for #{flutter_project_root_dir} done !!!")
861
+ puts("--------------------------------------------------------------------------------------")
761
862
 
762
863
  end
763
864
 
764
- # 启动一个资源变化监控服务,若检测到有资源变化,就自动执行generate操作;手动输入`Ctrl-C`,可终止当前服务
865
+ # 启动一个资源变化监控服务,若检测到flutter主工程和其子工程有资源变化,就自动执行generate_all操作;
866
+ # 手动输入`Ctrl-C`,可终止当前服务
765
867
  def self.start_monitor
766
868
 
767
- flutter_project_root_dir = FileUtil.get_cur_flutter_project_root_dir
869
+ flutter_main_project_root_dir = FileUtil.get_flutter_main_project_root_dir
768
870
 
769
871
  # ----- Step-1 Begin -----
770
- # 进行环境检测;若发现不合法的环境,则抛出异常,终止当前进程:
771
- # - 检测当前flutter工程根目录是否存在pubspec.yaml
772
- # - 检测当前pubspec.yaml中是否存在Flr的配置
773
- # - 检测当前flr_config中的resource_dir配置是否合法:
774
- # 判断合法的标准是:assets配置或者fonts配置了至少1个legal_resource_dir
872
+ # 对flutter主工程进行环境检测:
873
+ # - 检测当前flutter主工程根目录是否存在 pubspec.yaml
775
874
  #
776
875
 
777
876
  begin
778
- Checker.check_pubspec_file_is_existed(flutter_project_root_dir)
779
-
780
- pubspec_file_path = FileUtil.get_pubspec_file_path(flutter_project_root_dir)
781
-
782
- pubspec_config = FileUtil.load_pubspec_config_from_file(pubspec_file_path)
783
-
784
- Checker.check_flr_config_is_existed(pubspec_config)
785
-
786
- flr_config = pubspec_config["flr"]
787
-
788
- resource_dir_result_tuple = Checker.check_flr_assets_is_legal(flutter_project_root_dir, flr_config)
789
-
877
+ Checker.check_pubspec_file_is_existed(flutter_main_project_root_dir)
790
878
  rescue Exception => e
791
879
  puts(e.message)
792
880
  return
793
881
  end
794
882
 
795
- package_name = pubspec_config["name"]
796
-
797
883
  # ----- Step-1 End -----
798
884
 
885
+
799
886
  # ----- Step-2 Begin -----
800
- # 执行一次 flr generate 操作
887
+ # 对flutter工程进行合法资源目录检测:
888
+ # - 获取主工程的所有子工程根目录,生成工程根目录数组flutter_project_root_dir_array
889
+ # - 遍历flutter_project_root_dir_array,获取每个工程的legal_resource_dir数组:
890
+ # - 从flr_config中的assets配置获取assets_legal_resource_dir数组;
891
+ # - 从flr_config中的fonts配置获取fonts_legal_resource_dir数组;
892
+ # - 合并assets_legal_resource_dir数组和fonts_legal_resource_dir数组到legal_resource_dir数组。
893
+ # - 检测legal_resource_dir数组是否为空,为空则结束运行。
801
894
  #
895
+ puts("")
896
+ puts("get the valid resource directories of all projects now ...")
802
897
 
803
- now_str = Time.now.to_s
804
- puts("--------------------------- #{now_str} ---------------------------")
805
- puts("scan assets, specify scanned assets in pubspec.yaml, generate \"r.g.dart\" now ...")
806
- puts("\n")
807
- generate
808
- puts("\n")
809
- puts("scan assets, specify scanned assets in pubspec.yaml, generate \"r.g.dart\" done !!!")
810
- puts("---------------------------------------------------------------------------------")
811
- puts("\n")
898
+ flutter_main_project_root_dir = FileUtil.get_flutter_main_project_root_dir
899
+ flutter_sub_project_root_dir_array = FileUtil.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir)
900
+
901
+ flutter_project_root_dir_array = []
902
+ flutter_project_root_dir_array.push(flutter_main_project_root_dir)
903
+ flutter_project_root_dir_array += flutter_sub_project_root_dir_array
904
+
905
+ # 合法的资源目录数组
906
+ legal_resource_dir_array = []
907
+ # 非法的资源目录数组
908
+ illegal_resource_dir_array = []
909
+
910
+ flutter_project_root_dir_array.each do |flutter_project_root_dir|
911
+ begin
912
+ puts("")
913
+ puts("--------------------------- get info of specified project ----------------------------")
914
+ puts("get the valid resource directories from #{flutter_project_root_dir} now...")
915
+
916
+ Checker.check_pubspec_file_is_existed(flutter_project_root_dir)
917
+
918
+ pubspec_file_path = FileUtil.get_pubspec_file_path(flutter_project_root_dir)
919
+
920
+ pubspec_config = FileUtil.load_pubspec_config_from_file(pubspec_file_path)
921
+
922
+ Checker.check_flr_config_is_existed(pubspec_config)
923
+
924
+ flr_config = pubspec_config["flr"]
925
+
926
+ resource_dir_result_tuple = Checker.check_flr_assets_is_legal(flutter_project_root_dir, flr_config)
927
+
928
+ assets_legal_resource_dir_array = resource_dir_result_tuple[0]
929
+ fonts_legal_resource_dir_array = resource_dir_result_tuple[1]
930
+ legal_resource_dir_array += (assets_legal_resource_dir_array + fonts_legal_resource_dir_array)
931
+
932
+ illegal_resource_dir_array += resource_dir_result_tuple[2]
933
+ puts("get the valid resource directories from #{flutter_project_root_dir} done !!!")
934
+ puts("--------------------------------------------------------------------------------------")
935
+ rescue Exception => e
936
+ puts(e.message)
937
+ puts("[x]: #{flutter_project_root_dir} has no valid resource directories".error_style)
938
+ puts("--------------------------------------------------------------------------------------")
939
+ end
940
+ end
941
+
942
+ if legal_resource_dir_array.length <= 0
943
+ puts("")
944
+ puts("[x]: have no valid resource directories to be monitored".error_style)
945
+ return
946
+ end
947
+
948
+ puts("")
949
+ puts("get the valid resource directories of all projects done !!!")
812
950
 
813
951
  # ----- Step-2 End -----
814
952
 
953
+
815
954
  # ----- Step-3 Begin -----
816
- # 获取legal_resource_dir数组:
817
- # - 从flr_config中的assets配置获取assets_legal_resource_dir数组;
818
- # - 从flr_config中的fonts配置获取fonts_legal_resource_dir数组;
819
- # - 合并assets_legal_resource_dir数组和fonts_legal_resource_dir数组为legal_resource_dir数组。
955
+ # 执行一次 generate_all 操作
820
956
  #
821
957
 
822
- # 合法的资源目录数组
823
- assets_legal_resource_dir_array = resource_dir_result_tuple[0]
824
- fonts_legal_resource_dir_array = resource_dir_result_tuple[1]
825
-
826
- legal_resource_dir_array = assets_legal_resource_dir_array + fonts_legal_resource_dir_array
827
-
828
- # 非法的资源目录数组
829
- illegal_resource_dir_array = resource_dir_result_tuple[2]
958
+ puts("")
959
+ puts("now generate for all projects once before launching the monitoring service ...")
960
+ puts("")
961
+ generate_all
962
+ puts("")
963
+ puts("did generate for all projects, now is going to launching the monitoring service ...")
830
964
 
831
965
  # ----- Step-3 End -----
832
966
 
967
+
833
968
  # ----- Step-4 Begin -----
834
969
  # 启动资源监控服务
835
970
  # - 启动一个文件监控服务,对 legal_resource_dir 数组中的资源目录进行文件监控
836
971
  # - 若服务检测到资源变化(资源目录下的发生增/删/改文件),则执行一次 flr generate 操作
837
972
  #
838
973
 
974
+ puts("")
839
975
  now_str = Time.now.to_s
840
- puts("--------------------------- #{now_str} ---------------------------")
976
+ puts("----------------------------- #{now_str} -----------------------------")
841
977
  puts("launch a monitoring service now ...")
842
978
  puts("launching ...")
843
979
  # stop the monitoring service if exists
@@ -854,7 +990,7 @@ Flr recommends the following flutter resource structure schemes:
854
990
  puts(" - #{resource_dir}".warning_style)
855
991
  end
856
992
  end
857
- puts("---------------------------------------------------------------------------------")
993
+ puts("--------------------------------------------------------------------------------------")
858
994
  puts("\n")
859
995
 
860
996
  # Allow array of directories as input #92
@@ -862,16 +998,16 @@ Flr recommends the following flutter resource structure schemes:
862
998
  @@listener = Listen.to(*legal_resource_dir_array, ignore: [/\.DS_Store/], latency: 0.5, wait_for_delay: 5, relative: true) do |modified, added, removed|
863
999
  # for example: 2013-03-30 03:13:14 +0900
864
1000
  now_str = Time.now.to_s
865
- puts("--------------------------- #{now_str} ---------------------------")
1001
+ puts("----------------------------- #{now_str} -----------------------------")
866
1002
  puts("modified resource files: #{modified}")
867
1003
  puts("added resource files: #{added}")
868
1004
  puts("removed resource files: #{removed}")
869
- puts("scan assets, specify scanned assets in pubspec.yaml, generate \"r.g.dart\" now ...")
1005
+ puts("generate for all projects now ...")
870
1006
  puts("\n")
871
- generate
1007
+ generate_all
872
1008
  puts("\n")
873
- puts("scan assets, specify scanned assets in pubspec.yaml, generate \"r.g.dart\" done !!!")
874
- puts("---------------------------------------------------------------------------------")
1009
+ puts("generate for all projects done !!!")
1010
+ puts("--------------------------------------------------------------------------------------")
875
1011
  puts("\n")
876
1012
  puts("[*]: the monitoring service is monitoring the asset changes, and then auto scan assets, specifies assets and generates \"r.g.dart\" ...".tips_style)
877
1013
  puts("[*]: you can press \"Ctrl-C\" to terminate it".tips_style)
@@ -34,6 +34,162 @@ module Flr
34
34
  return false
35
35
  end
36
36
 
37
+ # is_image_asset?(asset) -> true or false
38
+ #
39
+ # 判断当前资产是不是图片类资产
40
+ #
41
+ # === Examples
42
+ #
43
+ # === Example-1
44
+ # asset = "packages/flutter_r_demo/assets/images/test.png"
45
+ # @return true
46
+ #
47
+ # === Example-2
48
+ # asset = "assets/images/test.png"
49
+ # @return true
50
+ #
51
+ def self.is_image_asset?(asset)
52
+ if FileUtil.is_image_resource_file?(asset)
53
+ return true
54
+ end
55
+
56
+ return false
57
+ end
58
+
59
+ # is_package_asset?(asset) -> true or false
60
+ #
61
+ # 判断当前资产是不是package类资产
62
+ #
63
+ # === Examples
64
+ #
65
+ # === Example-1
66
+ # asset = "packages/flutter_r_demo/assets/images/test.png"
67
+ # @return true
68
+ #
69
+ # === Example-2
70
+ # asset = "assets/images/test.png"
71
+ # @return false
72
+ #
73
+ def self.is_package_asset?(asset)
74
+ package_prefix = "packages/"
75
+ if asset =~ /\A#{package_prefix}/
76
+ return true
77
+ end
78
+
79
+ return false
80
+ end
81
+
82
+ # is_specified_package_asset?(package_name, asset) -> true or false
83
+ #
84
+ # 判断当前资产是不是指定的package的资产
85
+ #
86
+ # === Examples
87
+ # package_name = "flutter_r_demo"
88
+ #
89
+ # === Example-1
90
+ # asset = "packages/flutter_r_demo/assets/images/test.png"
91
+ # @return true
92
+ #
93
+ # === Example-2
94
+ # asset = "packages/hello_demo/assets/images/test.png"
95
+ # @return false
96
+ #
97
+ def self.is_specified_package_asset?(package_name, asset)
98
+ specified_package_prefix = "packages/" + package_name + "/"
99
+ if asset =~ /\A#{specified_package_prefix}/
100
+ return true
101
+ end
102
+
103
+ return false
104
+ end
105
+
106
+ # get_main_resource_file(flutter_project_dir, package_name, asset) -> main_resource_file
107
+ #
108
+ # 获取指定flutter工程的asset对应的主资源文件
109
+ # 注意:主资源文件不一定存在,比如图片资产可能只存在变体资源文件
110
+ #
111
+ # === Examples
112
+ # flutter_project_dir = "~/path/to/flutter_r_demo"
113
+ # package_name = "flutter_r_demo"
114
+ #
115
+ # === Example-1
116
+ # asset = "packages/flutter_r_demo/assets/images/test.png"
117
+ # main_resource_file = "~/path/to/flutter_r_demo/lib/assets/images/test.png"
118
+ #
119
+ # === Example-2
120
+ # asset = "assets/images/test.png"
121
+ # main_resource_file = "~/path/to/flutter_r_demo/assets/images/test.png"
122
+ #
123
+ def self.get_main_resource_file(flutter_project_dir, package_name, asset)
124
+ if is_specified_package_asset?(package_name, asset)
125
+ specified_package_prefix = "packages/" + package_name + "/"
126
+
127
+ # asset: packages/flutter_r_demo/assets/images/test.png
128
+ # to get implied_relative_resource_file: lib/assets/images/test.png
129
+ implied_relative_resource_file = asset.dup
130
+ implied_relative_resource_file[specified_package_prefix] = ""
131
+ implied_relative_resource_file = "lib/" + implied_relative_resource_file
132
+
133
+ # main_resource_file: ~/path/to/flutter_r_demo/lib/assets/images/test.png
134
+ main_resource_file = flutter_project_dir + "/" + implied_relative_resource_file
135
+ return main_resource_file
136
+ else
137
+ # asset: assets/images/test.png
138
+ # main_resource_file: ~/path/to/flutter_r_demo/assets/images/test.png
139
+ main_resource_file = flutter_project_dir + "/" + asset
140
+ return main_resource_file
141
+ end
142
+ end
143
+
144
+ # is_asset_existed?(flutter_project_dir, package_name, asset) -> true or false
145
+ #
146
+ # 判断指定flutter工程的asset是不是存在;存在的判断标准是:asset需要存在对应的资源文件
147
+ #
148
+ # === Examples
149
+ # flutter_project_dir = "~/path/to/flutter_r_demo"
150
+ # package_name = "flutter_r_demo"
151
+ #
152
+ # === Example-1
153
+ # asset = "packages/flutter_r_demo/assets/images/test.png"
154
+ # @return true
155
+ #
156
+ # === Example-2
157
+ # asset = "packages/flutter_r_demo/404/not-existed.png"
158
+ # @return false
159
+ #
160
+ def self.is_asset_existed?(flutter_project_dir, package_name, asset)
161
+ # 处理指定flutter工程的asset
162
+ # 1. 获取asset对应的main_resource_file
163
+ # 2. 若main_resource_file是非SVG类图片资源文件,判断asset是否存在的标准是:主资源文件或者至少一个变体资源文件存在
164
+ # 3. 若main_resource_file是SVG类图片资源文件或者其他资源文件,判断asset是否存在的标准是:主资源文件存在
165
+ #
166
+ main_resource_file = get_main_resource_file(flutter_project_dir, package_name, asset)
167
+ if FileUtil.is_non_svg_image_resource_file?(main_resource_file)
168
+ if File.exist?(main_resource_file)
169
+ return true
170
+ end
171
+
172
+ file_name = File.basename(main_resource_file)
173
+ file_dir = File.dirname(main_resource_file)
174
+ did_find_variant_resource_file = false
175
+ Dir.glob(["#{file_dir}/*/#{file_name}"]).each do |file|
176
+ if is_asset_variant?(file)
177
+ did_find_variant_resource_file = true
178
+ end
179
+ end
180
+
181
+ if did_find_variant_resource_file
182
+ return true
183
+ end
184
+ else
185
+ if File.exist?(main_resource_file)
186
+ return true
187
+ end
188
+ end
189
+
190
+ return false
191
+ end
192
+
37
193
  # generate_main_asset(flutter_project_dir, package_name, legal_resource_file) -> main_asset
38
194
  #
39
195
  # 为当前资源文件生成 main_asset
@@ -185,5 +341,47 @@ module Flr
185
341
  return font_asset_config_array
186
342
  end
187
343
 
344
+ # mergeFlutterAssets(new_asset_array, old_asset_array) -> merged_asset_array
345
+ #
346
+ # 合并新旧2个asset数组:
347
+ # - old_asset_array - new_asset_array = diff_asset_array,获取old_asset_array与new_asset_array的差异集合
348
+ # - 遍历diff_asset_array,筛选合法的asset得到legal_old_asset_array;合法的asset标准是:非图片资源 + 存在对应的资源文件
349
+ # - 按照字典序对legal_old_asset_array进行排序,并追加到new_asset_array
350
+ # - 返回合并结果merged_asset_array
351
+ #
352
+ # === Examples
353
+ # flutter_project_dir = "~/path/to/flutter_r_demo"
354
+ # package_name = "flutter_r_demo"
355
+ # new_asset_array = ["packages/flutter_r_demo/assets/images/test.png", "packages/flutter_r_demo/assets/jsons/test.json"]
356
+ # old_asset_array = ["packages/flutter_r_demo/assets/htmls/test.html"]
357
+ # merged_asset_array = ["packages/flutter_r_demo/assets/images/test.png", "packages/flutter_r_demo/assets/jsons/test.json", "packages/flutter_r_demo/assets/htmls/test.html"]
358
+ def self.mergeFlutterAssets(flutter_project_dir, package_name, new_asset_array, old_asset_array)
359
+ legal_old_asset_array = []
360
+
361
+ diff_asset_array = old_asset_array - new_asset_array;
362
+ diff_asset_array.each do |asset|
363
+ # 若是第三方package的资源,则合并到new_asset_array
364
+ # 引用第三方package的资源的推荐做法是:通过引用第三方package的R类来访问
365
+ if is_package_asset?(asset)
366
+ if is_specified_package_asset?(package_name, asset) == false
367
+ legal_old_asset_array.push(asset)
368
+ next
369
+ end
370
+ end
371
+
372
+ # 处理指定flutter工程的asset
373
+ # 1. 判断asset是否存在
374
+ # 2. 若asset存在,则合并到new_asset_array
375
+ #
376
+ if is_asset_existed?(flutter_project_dir, package_name, asset)
377
+ legal_old_asset_array.push(asset)
378
+ end
379
+ end
380
+
381
+ legal_old_asset_array.sort!
382
+ merged_asset_array = new_asset_array + legal_old_asset_array
383
+ return merged_asset_array
384
+ end
385
+
188
386
  end
189
387
  end
@@ -8,13 +8,26 @@ module Flr
8
8
 
9
9
  # get_cur_flutter_project_root_dir -> String
10
10
  #
11
- # 获取当前flutter工程的根目录
11
+ # 获取flutter主工程的根目录
12
12
  #
13
- def self.get_cur_flutter_project_root_dir
13
+ def self.get_flutter_main_project_root_dir
14
14
  flutter_project_root_dir = "#{Pathname.pwd}"
15
15
  return flutter_project_root_dir
16
16
  end
17
17
 
18
+ # get_flutter_sub_project_root_dirs -> [sub_project_root_dir]
19
+ #
20
+ # 获取flutter主工程的所有子工程的根目录
21
+ #
22
+ def self.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir)
23
+ flutter_sub_project_root_dir_array = []
24
+ Dir.glob(["#{flutter_main_project_root_dir}/*/pubspec.yaml"]).each do |file|
25
+ flutter_project_root_dir = File.dirname(file)
26
+ flutter_sub_project_root_dir_array.push(flutter_project_root_dir)
27
+ end
28
+ return flutter_sub_project_root_dir_array
29
+ end
30
+
18
31
  # get_pubspec_file_path(flutter_project_dir) -> String
19
32
  #
20
33
  # 获取当前flutter工程的pubspec.yaml文件的路径
@@ -1,9 +1,9 @@
1
1
  module Flr
2
2
  # 工具版本号
3
- VERSION = "3.0.0"
3
+ VERSION = "3.1.0"
4
4
 
5
5
  # 核心逻辑版本号
6
- CORE_VERSION = "3.0.0"
6
+ CORE_VERSION = "3.1.0"
7
7
 
8
8
  class Version < Array
9
9
  def initialize str
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flr
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - York
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-21 00:00:00.000000000 Z
11
+ date: 2020-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler