Class: Flr::FileUtil
- Inherits:
-
Object
- Object
- Flr::FileUtil
- Defined in:
- lib/flr/util/file_util.rb
Overview
资源文件相关的工具类方法
Class Method Summary collapse
-
.dump_pubspec_config_to_file(pubspec_config, pubspec_file_path) ⇒ Object
dump_pubspec_config_to_file -> true.
-
.find_font_files_in_font_family_dir(font_family_dir) ⇒ Object
find_font_files_in_font_family_dir(font_family_dir) -> font_file_result_tuple.
-
.find_image_files(resource_dir) ⇒ Object
find_image_files(resource_dir) -> image_file_result_tuple.
-
.find_text_files(resource_dir) ⇒ Object
find_text_files(resource_dir) -> text_file_result_tuple.
-
.find_top_child_dirs(resource_dir) ⇒ Object
find_top_child_dirs(resource_dir) -> top_child_dir_array.
-
.get_flutter_main_project_root_dir ⇒ Object
get_cur_flutter_project_root_dir -> String.
-
.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir) ⇒ Object
get_flutter_sub_project_root_dirs -> [sub_project_root_dir].
-
.get_pubspec_file_path(flutter_project_dir) ⇒ Object
get_pubspec_file_path(flutter_project_dir) -> String.
-
.is_font_resource_file?(file) ⇒ Boolean
判断当前文件是不是字体资源文件.
-
.is_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是图片资源文件.
-
.is_legal_resource_file?(file) ⇒ Boolean
is_legal_resource_file??(file) -> true or false.
-
.is_non_svg_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是非SVG类图片资源文件.
-
.is_package_project_type?(flutter_project_dir) ⇒ Boolean
is_package_project_type?(flutter_project_dir) -> true or false.
-
.is_svg_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是SVG类图片资源文件.
-
.is_text_resource_file?(file) ⇒ Boolean
判断当前文件是不是文本资源文件.
-
.load_pubspec_config_from_file(pubspec_file_path) ⇒ Object
load_pubspec_config_from_file -> Hash.
Class Method Details
.dump_pubspec_config_to_file(pubspec_config, pubspec_file_path) ⇒ Object
dump_pubspec_config_to_file -> true
保存pubspec_config到pubspec.yaml
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/flr/util/file_util.rb', line 76 def self.dump_pubspec_config_to_file(pubspec_config, pubspec_file_path) pubspec_file = File.open(pubspec_file_path, 'w') yaml_content = pubspec_config.to_yaml # Because pubspec.yaml is only one document remove, # and I want to shortcut it, # so I choose to remove three dashes (“---”). # # To get the details about three dashes (“---”) # see: https://yaml.org/spec/1.2/spec.html#id2760395 # document_separate_maker = "---\n" regx = /\A#{document_separate_maker}/ if yaml_content =~ regx yaml_content[document_separate_maker] = "" end pubspec_file.write(yaml_content) pubspec_file.close return true end |
.find_font_files_in_font_family_dir(font_family_dir) ⇒ Object
find_font_files_in_font_family_dir(font_family_dir) -> font_file_result_tuple
扫描指定的字体家族目录和其所有层级的子目录,查找所有字体文件 返回字体文件结果二元组 font_file_result_tuple font_file_result_tuple = [legal_font_file_array, illegal_font_file_array]
判断文件合法的标准参考 self.is_legal_resource_file? 方法
Examples
font_family_dir = “~/path/to/flutter_project/lib/assets/fonts/Amiri” legal_font_file_array = [“~/path/to/flutter_project/lib/assets/fonts/Amiri/Amiri-Regular.ttf”, “~/path/to/flutter_project/lib/assets/fonts/Amiri/Amiri-Bold.ttf”] illegal_font_file_array = [“~/path/to/flutter_project/lib/assets/fonts/Amiri/~.ttf”]
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/flr/util/file_util.rb', line 333 def self.find_font_files_in_font_family_dir(font_family_dir) legal_font_file_array = [] illegal_font_file_array = [] pattern_file_types = Flr::FONT_FILE_TYPES.join(",") # dir/**/*{.ttf.,.ott} : 查找当前目录和其所有子目录的指定类型文件 Dir.glob(["#{font_family_dir}/**/*{#{pattern_file_types}}"]).each do |file| if is_legal_resource_file?(file) legal_font_file_array.push(file) else illegal_font_file_array.push(file) end end font_file_result_tuple = [legal_font_file_array, illegal_font_file_array] return font_file_result_tuple end |
.find_image_files(resource_dir) ⇒ Object
find_image_files(resource_dir) -> image_file_result_tuple
扫描指定的资源目录和其所有层级的子目录,查找所有图片文件 返回图片文件结果二元组 image_file_result_tuple image_file_result_tuple = [legal_image_file_array, illegal_image_file_array]
判断文件合法的标准参考 self.is_legal_resource_file? 方法
Examples
resource_dir = “~/path/to/flutter_project/lib/assets/images” legal_image_file_array = [“~/path/to/flutter_project/lib/assets/images/test.png”, “~/path/to/flutter_project/lib/assets/images/2.0x/test.png”] illegal_image_file_array = [“~/path/to/flutter_project/lib/assets/images/~.png”]
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/flr/util/file_util.rb', line 249 def self.find_image_files(resource_dir) legal_image_file_array = [] illegal_image_file_array = [] pattern_file_types = Flr::IMAGE_FILE_TYPES.join(",") # dir/*{.png,.jpg} : 查找当前目录的指定类型文件 # dir/*/*{.png,.jpg}: 查找当前目录的第1级子目录的指定类型文件 # dir/**/*{.png,.jpg}: 查找当前目录和其所有子目录的指定类型文件 Dir.glob(["#{resource_dir}/**/*{#{pattern_file_types}}"]).each do |file| if is_legal_resource_file?(file) legal_image_file_array.push(file) else illegal_image_file_array.push(file) end end image_file_result_tuple = [legal_image_file_array, illegal_image_file_array] return image_file_result_tuple end |
.find_text_files(resource_dir) ⇒ Object
find_text_files(resource_dir) -> text_file_result_tuple
扫描指定的资源目录和其所有层级的子目录,查找所有文本文件 返回文本文件结果二元组 text_file_result_tuple text_file_result_tuple = [legal_text_file_array, illegal_text_file_array]
判断文件合法的标准参考 self.is_legal_resource_file? 方法
Examples
resource_dir = “~/path/to/flutter_project/lib/assets/jsons” legal_text_file_array = [“~/path/to/flutter_project/lib/assets/jsons/city.json”, “~/path/to/flutter_project/lib/assets/jsons/mock/city.json”] illegal_text_file_array = [“~/path/to/flutter_project/lib/assets/jsons/~.json”]
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/flr/util/file_util.rb', line 282 def self.find_text_files(resource_dir) legal_text_file_array = [] illegal_text_file_array = [] pattern_file_types = Flr::TEXT_FILE_TYPES.join(",") # dir/**/*{.json.,.yaml} : 查找当前目录和其所有子目录的指定类型文件 Dir.glob(["#{resource_dir}/**/*{#{pattern_file_types}}"]).each do |file| if is_legal_resource_file?(file) legal_text_file_array.push(file) else illegal_text_file_array.push(file) end end text_file_result_tuple = [legal_text_file_array, illegal_text_file_array] return text_file_result_tuple end |
.find_top_child_dirs(resource_dir) ⇒ Object
find_top_child_dirs(resource_dir) -> top_child_dir_array
扫描指定的资源目录,返回其所有第一级子目录
Examples
resource_dir = “~/path/to/flutter_project/lib/assets/fonts” top_child_dir_array = [“~/path/to/flutter_project/lib/assets/fonts/Amiri”, “~/path/to/flutter_project/lib/assets/fonts/Open_Sans”]
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/flr/util/file_util.rb', line 308 def self.find_top_child_dirs(resource_dir) top_child_dir_array = [] Dir.glob(["#{resource_dir}/*"]).each do |file| if File.directory?(file) top_child_dir_array.push(file) end end return top_child_dir_array end |
.get_flutter_main_project_root_dir ⇒ Object
get_cur_flutter_project_root_dir -> String
获取flutter主工程的根目录
13 14 15 16 |
# File 'lib/flr/util/file_util.rb', line 13 def self.get_flutter_main_project_root_dir flutter_project_root_dir = "#{Pathname.pwd}" return flutter_project_root_dir end |
.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir) ⇒ Object
get_flutter_sub_project_root_dirs -> [sub_project_root_dir]
获取flutter主工程的所有子工程的根目录
22 23 24 25 26 27 28 29 |
# File 'lib/flr/util/file_util.rb', line 22 def self.get_flutter_sub_project_root_dirs(flutter_main_project_root_dir) flutter_sub_project_root_dir_array = [] Dir.glob(["#{flutter_main_project_root_dir}/*/pubspec.yaml"]).each do |file| flutter_project_root_dir = File.dirname(file) flutter_sub_project_root_dir_array.push(flutter_project_root_dir) end return flutter_sub_project_root_dir_array end |
.get_pubspec_file_path(flutter_project_dir) ⇒ Object
get_pubspec_file_path(flutter_project_dir) -> String
获取当前flutter工程的pubspec.yaml文件的路径
Examples
flutter_project_dir = “~/path/to/flutter_r_demo” pubspec_file_path = “~/path/to/flutter_r_demo/pubspec.yaml”
39 40 41 42 |
# File 'lib/flr/util/file_util.rb', line 39 def self.get_pubspec_file_path(flutter_project_dir) file_path = flutter_project_dir + "/pubspec.yaml" return file_path end |
.is_font_resource_file?(file) ⇒ Boolean
判断当前文件是不是字体资源文件
202 203 204 205 206 207 208 209 210 |
# File 'lib/flr/util/file_util.rb', line 202 def self.is_font_resource_file?(file) file_extname = File.extname(file).downcase if Flr::FONT_FILE_TYPES.include?(file_extname) return true; end return false end |
.is_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是图片资源文件
180 181 182 183 184 185 186 187 188 |
# File 'lib/flr/util/file_util.rb', line 180 def self.is_image_resource_file?(file) file_extname = File.extname(file).downcase if Flr::IMAGE_FILE_TYPES.include?(file_extname) return true; end return false end |
.is_legal_resource_file?(file) ⇒ Boolean
is_legal_resource_file??(file) -> true or false
判断当前资源文件是否合法
判断资源文件合法的标准是: 其file_basename_no_extension 由字母(a-z、A-Z)、数字(0-9)、其他合法字符(‘_’, ‘+’, ‘-’, ‘.’, ‘·’, ‘!’, ‘@’, ‘&’, ‘$’, ‘¥’)组成
Examples
good_file = “~/path/to/flutter_project/lib/assets/images/test.png” bad_file = “~/path/to/flutter_project/lib/assets/images/~.png” is_legal_resource_file?(good_file) -> true is_legal_resource_file?(bad_file) -> false
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/flr/util/file_util.rb', line 225 def self.is_legal_resource_file?(file) file_basename_no_extension = File.basename(file, ".*") regx = /^[a-zA-Z0-9_\+\-\.·!@&$¥]+$/ if file_basename_no_extension =~ regx return true else return false end end |
.is_non_svg_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是非SVG类图片资源文件
158 159 160 161 162 163 164 165 166 |
# File 'lib/flr/util/file_util.rb', line 158 def self.is_non_svg_image_resource_file?(file) file_extname = File.extname(file).downcase if Flr::NON_SVG_IMAGE_FILE_TYPES.include?(file_extname) return true; end return false end |
.is_package_project_type?(flutter_project_dir) ⇒ Boolean
is_package_project_type?(flutter_project_dir) -> true or false
判断当前flutter工程的工程类型是不是Package工程类型
flutter工程共有4种工程类型:
-
app:Flutter App工程,用于开发纯Flutter的App
-
module:Flutter Component工程,用于开发Flutter组件以嵌入iOS和Android原生工程
-
package:General Dart Package工程,用于开发一个供应用层开发者使用的包
-
plugin:Plugin Package工程(属于特殊的Dart Package工程),用于开发一个调用特定平台API的包
flutter工程的工程类型可从flutter工程目录的 .metadata 文件中读取获得 如果不存在 .metadata 文件,则判断 pubspec.yaml 是否存在 author 配置,若存在,说明是一个 Package工程
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/flr/util/file_util.rb', line 111 def self.is_package_project_type?(flutter_project_dir) = flutter_project_dir + "/.metadata" if File.exist?() begin = File.open(, 'r') = YAML.load() project_type = ["project_type"] if project_type.nil? project_type = "unknown" end project_type = project_type.downcase if project_type == "package" || project_type == "plugin" return true end rescue YAML::SyntaxError => e puts("YAML Syntax Error: #{e}".error_style) puts("") ensure .close end else = <<-MESSAGE #{"[!]: warning, metadata file is missed, flr can not make sure to get a right project type of this flutter project".warning_style} #{"[!]: then flr maybe generate buggy r.g.dart".warning_style} #{"[*]: to fix it, you can manually copy the metadata file of a flutter project with same project type to #{}".tips_style} MESSAGE puts() begin pubspec_file_path = get_pubspec_file_path(flutter_project_dir) pubspec_config = load_pubspec_config_from_file(pubspec_file_path) if pubspec_config.has_key?("author") return true end rescue Exception => e puts(e.) end end return false end |
.is_svg_image_resource_file?(file) ⇒ Boolean
判断当前文件是不是SVG类图片资源文件
169 170 171 172 173 174 175 176 177 |
# File 'lib/flr/util/file_util.rb', line 169 def self.is_svg_image_resource_file?(file) file_extname = File.extname(file).downcase if Flr::SVG_IMAGE_FILE_TYPES.include?(file_extname) return true; end return false end |
.is_text_resource_file?(file) ⇒ Boolean
判断当前文件是不是文本资源文件
191 192 193 194 195 196 197 198 199 |
# File 'lib/flr/util/file_util.rb', line 191 def self.is_text_resource_file?(file) file_extname = File.extname(file).downcase if Flr::TEXT_FILE_TYPES.include?(file_extname) return true; end return false end |
.load_pubspec_config_from_file(pubspec_file_path) ⇒ Object
load_pubspec_config_from_file -> Hash
读取pubspec.yaml到pubspec_config 若读取成功,返回一个Hash对象pubspec_config 若读取失败,则抛出异常
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flr/util/file_util.rb', line 50 def self.load_pubspec_config_from_file(pubspec_file_path) begin pubspec_file = File.open(pubspec_file_path, 'r') pubspec_config = YAML.load(pubspec_file) rescue YAML::SyntaxError => e puts("YAML Syntax Error: #{e}".error_style) puts("") = <<-MESSAGE #{"[x]: pubspec.yaml is damaged with syntax error".error_style} #{"[*]: please correct the pubspec.yaml file at #{pubspec_file_path}".tips_style} MESSAGE raise() ensure pubspec_file.close end return pubspec_config end |