进出入完善组织机构并加入导入人员和机构功能
554325746@qq.com
2019-08-07 07a66e53d2b4126c2004870d81a379d8ef0071da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%%%-------------------------------------------------------------------
%%% @author bsk
%%% @copyright (C) 2018, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 13. 十二月 2018 下午1:43
%%%-------------------------------------------------------------------
 
%%%-------------------------------------------------------------------
%%% @author zhongwencool@gmail.com
%%% @doc   把对应的.config文件转为.beam文件并更新到内存
%%%        configName:all()       return all key_value
%%%        configName:lookup(Key) return value or undefined
%%% 1.得到目录下文件集合
%%% 2.遍历对每个文件进行转换
%%%   2.1 把文件读入 检查格式
%%%   2.2 把config转化为erl文件
%%%   2.2 把erl文件编译成beam文件
%%% 3.加载beam文件到内存
%%% @end
%%% Created : 22. Aug 2014 4:08 PM
%%%-------------------------------------------------------------------
-module(configToBeam).
-author("bsk").
 
-compile(export_all).
%% API
-export([config_to_beam/0, config_to_beam/1, get_config_dir/0]).
 
%% @doc 把指定目录下全部的config文件都转为.beam文件并加载到内存
config_to_beam() ->
  AllConfigName = get_all_config_name(),
  io:format("~p ~n", [AllConfigName]),
  [begin
     config_to_beam(ConfigName)
   end || ConfigName <- AllConfigName].
 
%% @doc 把指定目录下名为ConfigName的config文件转为.beam文件并加载到内存
config_to_beam(ConfigName) ->
  case check_syntax(ConfigName) of
    {ok, TermList} ->
      term_to_beam(ConfigName -- ".config", TermList);
    Reason ->
      io:format("check Config syntax error~p:Reason:~p~n", [ConfigName, Reason]),
      {Reason, ConfigName}
  end.
 
%% @todo 改成你存放.config文件的目录
get_config_dir() ->
%%  code:root_dir() ++ "/config".
  "config".
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  Internal Function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
term_to_beam(ModuleName, TermList) ->
  Erl = term_to_erl(ModuleName, TermList),
  ModuleName2 = ModuleName ++ ".erl",
  file:write_file(ModuleName2, Erl, [write, binary, {encoding, utf8}]),
  compile:file(ModuleName2),
  ModuleName3 = list_to_atom(ModuleName),
  code:purge(ModuleName3),
  code:load_file(ModuleName3),
  {ok, ModuleName3}.
 
term_to_erl(ModuleName, TermList) ->
  StrValue = lists:flatten(term_to_erl2(TermList, "")),
  StrList = lists:flatten(io_lib:format("     ~w\n", [TermList])),
  "
  -module(" ++ ModuleName ++ ").
     -export([all/0,lookup/1]).
 
     all()->" ++ StrList ++ ".
 
     lookup(Key) ->
       case Key of
         " ++ StrValue ++ "
         _ -> undefined
     end.
 ".
 
term_to_erl2([], Sum) ->
  Sum;
term_to_erl2([{Key, Value} | Left], Acc) ->
  term_to_erl2(Left,
    io_lib:format("       ~w -> ~w;\n", [Key, Value]) ++ Acc).
 
get_all_config_name() ->
%%  {ok, AllFileName} = file:list_dir(get_config_dir()),
  {ok, AllFileName} = file:list_dir("config"),
  io:format("~p~n", [AllFileName]),
  lists:filter(fun(FileName) ->
    A = lists:reverse(FileName),
    case A of
      "gifnoc." ++ _ ->
        io:format("~p~n", [A]), true;
      _ ->
        io:format("~p~n", [A]), false
    end
               end, AllFileName).
 
check_syntax(ConfigName) ->
  FilePath = joint_path(ConfigName),
  io:format("FilePath ~p ~n", [FilePath]),
  Res = file:consult(FilePath),
  io:format("Res ~p ~n", [Res]),
  case Res of
    {ok, TermList = [_ | _]} ->
      io:format("TermList ~p ~n", [TermList]),
      check_fromat_duplicate(TermList, []);
    Reason ->
      io:format("Reason ~p ~n", [Reason]),
      {error, Reason}
  end.
 
joint_path(ConfigName) ->
  get_config_dir() ++ "/" ++ ConfigName.
 
check_fromat_duplicate([], Acc) ->
  {ok, Acc};
check_fromat_duplicate([{Key, _Value} = Term | Left], Acc) ->
  case lists:keymember(Key, 1, Acc) of
    true -> {error, key_duplicate, Key};
    false -> check_fromat_duplicate(Left, [Term | Acc])
  end;
check_fromat_duplicate([Term | _], _Acc) ->
  {error, format_error, Term}.