1
1
-module (divapi_component ).
2
2
3
- -export ([tags /1 , diversity_json /2 , settings /2 , settingsForm /2 , file /3 , css /3 , thumbnail /1 ]).
3
+ -export ([tags /1 , diversity_json /2 , settings /2 , settingsForm /2 , file /3 , css /3 , thumbnail /1 ,
4
+ dir /1 , dir /2 , exists /1 , exists /2 ]).
4
5
5
6
% % @doc Retrive all tags for a given component
6
- tags (ComponentName ) ->
7
- git_utils :tags (ComponentName ).
7
+ tags (ComponentPath ) ->
8
+ git_utils :tags (ComponentPath ).
8
9
9
10
% % @doc Fetches the diversity json for a spcific component/tag. Will cache the result.
10
- diversity_json (ComponentName , Tag ) ->
11
+ diversity_json (ComponentPath , Tag ) ->
11
12
divapi_cache :get (
12
- {diversity_json , ComponentName , Tag },
13
- fun () -> jiffy :decode (git_utils : get_diversity_json ( ComponentName , Tag )) end ,
13
+ {diversity_json , ComponentPath , Tag },
14
+ fun () -> jiffy :decode (file ( ComponentPath , Tag ,<< " diversity.json " >> )) end ,
14
15
1000 * 60 * 60 * 5 % 5 hours
15
16
).
16
17
17
18
% % @doc Serve the settings from diversity.json
18
- settings (ComponentName , Tag ) ->
19
- case diversity_json (ComponentName , Tag ) of
19
+ settings (ComponentPath , Tag ) ->
20
+ case diversity_json (ComponentPath , Tag ) of
20
21
undefined ->
21
22
resource_not_found ;
22
23
Json ->
@@ -27,8 +28,8 @@ settings(ComponentName, Tag) ->
27
28
end .
28
29
29
30
% % @doc Serve the settingsForm from diversity.json
30
- settingsForm (ComponentName , Tag ) ->
31
- case diversity_json (ComponentName , Tag ) of
31
+ settingsForm (ComponentPath , Tag ) ->
32
+ case diversity_json (ComponentPath , Tag ) of
32
33
undefined ->
33
34
resource_not_found ;
34
35
Json ->
@@ -38,26 +39,34 @@ settingsForm(ComponentName, Tag) ->
38
39
SettingsJson
39
40
end .
40
41
41
-
42
- % % @doc Serve an arbitrary file from the repository
43
-
44
- file (ComponentName , Tag , File ) ->
45
- case git_utils :get_file (ComponentName , Tag , File ) of
46
- undefined ->
47
- resource_not_found ;
48
- error ->
49
- % % Should get a reason from git_utils get_file command.
50
- {error , <<" Error while fetching file" >>};
51
- FileBin ->
52
- FileBin
42
+ % % @doc Serve an arbitrary file for the component
43
+ file (ComponentPath , Tag , File ) ->
44
+ case divapi_app :is_production () of
45
+ true ->
46
+ % % Read the file!!
47
+ Path = filename :join (ComponentPath , File ),
48
+ case file :read (Path ) of
49
+ {ok , FileBin } -> FileBin ;
50
+ _ -> resource_not_found
51
+ end ;
52
+ false ->
53
+ case git_utils :get_file (ComponentPath , Tag , File ) of
54
+ undefined ->
55
+ resource_not_found ;
56
+ error ->
57
+ % % Should get a reason from git_utils get_file command.
58
+ {error , <<" Error while fetching file" >>};
59
+ FileBin ->
60
+ FileBin
61
+ end
53
62
end .
54
63
55
64
% % @doc Serve all css and sass (concatenated into one file)
56
65
% % This function will check out the diversity.json-file from the components repository
57
66
% % and check the styles-property to find all the css- and sass-files. The sass files are
58
67
% % first compiled then all css is concatenated and returned to the user.
59
- css (ComponentName , Tag , Variables0 ) ->
60
- case git_utils :get_diversity_json (ComponentName , Tag ) of
68
+ css (ComponentPath , Tag , Variables0 ) ->
69
+ case git_utils :get_diversity_json (ComponentPath , Tag ) of
61
70
undefined ->
62
71
resource_not_found ;
63
72
DiversityData ->
@@ -72,42 +81,64 @@ css(ComponentName, Tag, Variables0) ->
72
81
% % To get a consitent cache key we need to make sure the proplist is sorted
73
82
Variables1 = lists :sort (Variables0 ),
74
83
Files = divapi_cache :get (
75
- {css , ComponentName , Tag , Variables1 },
76
- fun () -> get_css (ComponentName , Tag , Variables1 , StylePaths ) end ,
84
+ {css , ComponentPath , Tag , Variables1 },
85
+ fun () -> get_css (ComponentPath , Tag , Variables1 , StylePaths ) end ,
77
86
1000 * 60 * 60 * 5 % 5 hours
78
87
),
79
88
{css , Files }
80
89
end .
81
90
82
- thumbnail (ComponentName ) ->
83
- case git_utils :get_diversity_json (ComponentName , <<" *" >>) of
91
+ thumbnail (ComponentPath ) ->
92
+ case git_utils :get_diversity_json (ComponentPath , <<" *" >>) of
84
93
undefined ->
85
94
resource_not_found ;
86
95
Json ->
87
96
{DiversityData } = jiffy :decode (Json ),
88
97
ThumbnailPath = proplists :get_value (<<" thumbnail" >>, DiversityData , undefined ),
89
- file (ComponentName , <<" *" >>, ThumbnailPath )
98
+ file (ComponentPath , <<" *" >>, ThumbnailPath )
90
99
end .
91
100
92
101
% % @doc Retrive al
93
- get_css (ComponentName , Tag , Variables , Paths ) ->
102
+ get_css (ComponentPath , Tag , Variables , Paths ) ->
94
103
% % Retrive all files(CSS and SCSS).
95
104
% % Compile all SCSS-files to CSS and then concatenate all of them
96
105
GetFile = fun (Path ) ->
97
106
% % Retrive the temporary checked out file from the repository
98
- File = git_utils : get_file ( ComponentName , Tag , Path ),
107
+ File = file ( ComponentPath , Tag , Path ),
99
108
case filename :extension (Path ) of
100
109
<<" .scss" >> ->
101
- sass_compile (ComponentName , Tag , Path , File , Variables );
110
+ sass_compile (ComponentPath , Tag , Path , File , Variables );
102
111
<<" .css" >> ->
103
112
File
104
113
end
105
114
end ,
106
115
lists :map (GetFile , Paths ).
107
116
117
+ dir (ComponentPath ) ->
118
+ {ok , RepoDir } = application :get_env (divapi , repo_dir ),
119
+ RepoDir1 = case is_binary (RepoDir ) of
120
+ true -> RepoDir ;
121
+ false -> unicode :character_to_binary (RepoDir )
122
+ end ,
123
+ <<RepoDir1 /binary , " /" , ComponentPath /binary , " .git" >>.
124
+
125
+ dir (ComponentPath , Stage ) ->
126
+ {ok , RepoDir } = application :get_env (divapi , repo_dir ),
127
+ RepoDir1 = case is_binary (RepoDir ) of
128
+ true -> RepoDir ;
129
+ false -> unicode :character_to_binary (RepoDir )
130
+ end ,
131
+ <<RepoDir1 /binary , " /" , Stage /binary , " /" , ComponentPath /binary , " .git" >>.
132
+
133
+ exists (ComponentPath ) ->
134
+ filelib :is_dir (dir (ComponentPath )).
135
+
136
+ exists (ComponentPath , Stage ) ->
137
+ lager :info (" Eeeeeh... ~p " , [dir (ComponentPath , Stage )]),
138
+ filelib :is_dir (dir (ComponentPath , Stage )).
108
139
109
140
% % @doc Compile a sass-file with given variables
110
- sass_compile (ComponentName , Tag , Path , File , Variables ) ->
141
+ sass_compile (ComponentPath , Tag , Path , File , Variables ) ->
111
142
112
143
% % Construct the SASS-variables
113
144
VarToBinary = fun ({Variable , Value }, BinAcc ) ->
@@ -117,7 +148,7 @@ sass_compile(ComponentName, Tag, Path, File, Variables) ->
117
148
BinaryVars = lists :foldl (VarToBinary , <<>>, Variables ),
118
149
119
150
% % Create a temporary sass file
120
- TmpName0 = {ComponentName , Tag , Path , Variables },
151
+ TmpName0 = {ComponentPath , Tag , Path , Variables },
121
152
TmpName1 = integer_to_binary (erlang :phash2 (TmpName0 )),
122
153
TmpPath = filename :join (<<" /tmp/divapi/" >>, TmpName1 ),
123
154
ok = filelib :ensure_dir (TmpPath ),
0 commit comments