@@ -2,30 +2,21 @@ open! Core
22open ! Async
33
44module Serve = struct
5- type enabled =
5+ type t =
66 { port : int
77 ; perfetto_ui_base_directory : string
88 }
99
10- type t =
11- | Disabled
12- | Enabled of enabled
13-
14- let param =
15- match Env_vars. perfetto_ui_base_directory with
16- | None -> Command.Param. return Disabled
17- | Some perfetto_ui_base_directory ->
18- let % map_open.Command port =
19- let default = 8080 in
20- flag
21- " serve"
22- (optional_with_default default int )
23- ~doc:
24- [% string
25- " PORT Chooses the port that the local copy of Perfetto will be served on. \
26- (default: %{default#Int})" ]
27- in
28- Enabled { port; perfetto_ui_base_directory }
10+ let maybe_param =
11+ Option. map Env_vars. perfetto_ui_base_directory ~f: (fun perfetto_ui_base_directory ->
12+ let port = 8080 in
13+ let % map_open.Command serve =
14+ flag
15+ " serve"
16+ no_arg
17+ ~doc: [% string " Serves the magic-trace UI on port %{port#Int})" ]
18+ in
19+ if serve then Some { port; perfetto_ui_base_directory } else None )
2920 ;;
3021
3122 let url t =
@@ -110,8 +101,35 @@ module Serve = struct
110101 ;;
111102end
112103
104+ module Share = struct
105+ type t = { share_command_filename : string }
106+
107+ let maybe_param =
108+ Option. map Env_vars. share_command_filename ~f: (fun share_command_filename ->
109+ let % map_open.Command share = flag " share" no_arg ~doc: " Share trace." in
110+ if share then Some { share_command_filename } else None )
111+ ;;
112+
113+ let share_trace_file { share_command_filename } ~store_path =
114+ let % map.Deferred. Or_error output =
115+ Process. run
116+ ~prog: share_command_filename
117+ ~args: [ Filename_unix. realpath store_path ]
118+ ()
119+ in
120+ Core. printf " %s%!" output
121+ ;;
122+ end
123+
124+ module Display_mode = struct
125+ type t =
126+ | Serve of Serve .t
127+ | Share of Share .t
128+ | Disabled
129+ end
130+
113131type t =
114- { serve : Serve .t
132+ { display_mode : Display_mode .t
115133 ; store_path : string
116134 }
117135
@@ -121,9 +139,18 @@ let param =
121139 flag
122140 " output"
123141 (optional_with_default default string )
142+ ~aliases: [ " o" ]
124143 ~doc: [% string " FILE Where to output the trace. (default: '%{default}')" ]
125- and serve = Serve. param in
126- { serve; store_path }
144+ and display_mode =
145+ [ Serve. maybe_param
146+ |> Option. map ~f: (map ~f: (Option. map ~f: (fun serve -> Display_mode. Serve serve)))
147+ ; Share. maybe_param
148+ |> Option. map ~f: (map ~f: (Option. map ~f: (fun share -> Display_mode. Share share)))
149+ ]
150+ |> List. filter_opt
151+ |> choose_one ~if_nothing_chosen: (Default_to Display_mode. Disabled )
152+ in
153+ { display_mode; store_path }
127154;;
128155
129156let notify_trace ~store_path =
@@ -132,9 +159,10 @@ let notify_trace ~store_path =
132159;;
133160
134161let maybe_serve t ~filename =
135- match t.serve with
162+ match t.display_mode with
136163 | Disabled -> notify_trace ~store_path: t.store_path
137- | Enabled serve -> Serve. serve_trace_file serve ~filename ~store_path: t.store_path
164+ | Serve serve -> Serve. serve_trace_file serve ~filename ~store_path: t.store_path
165+ | Share share -> Share. share_trace_file share ~store_path: t.store_path
138166;;
139167
140168let maybe_stash_old_trace ~filename =
0 commit comments