[doc] Add WatchfaceComplication to the doc index (#587)
[platform/core/csapi/tizenfx.git] / tools / src / GenDummy.Tasks / GenDummyTask.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.IO;
19 using System.Collections.Generic;
20 using Microsoft.Build.Framework;
21 using Microsoft.Build.Utilities;
22
23 namespace GenDummy.Tasks
24 {
25     public class GenDummyTask : ITask
26     {
27         readonly List<ITaskItem> _generatedFiles = new List<ITaskItem>();
28
29         public IBuildEngine BuildEngine { get; set; }
30         public ITaskHost HostObject { get; set; }
31
32         [Required]
33         public ITaskItem[] Sources { get; set; }
34
35         [Required]
36         public string OutputDirectory { get; set; }
37
38         [Output]
39         public ITaskItem[] GeneratedFiles => _generatedFiles.ToArray();
40
41         public bool Execute()
42         {
43             try
44             {
45                 ExecuteCore().Wait();
46             } catch(Exception e)
47             {
48                 Console.Error.WriteLine($"{e.ToString()} : {e.Message}");
49                 return false;
50             }
51             return true;
52         }
53
54         public async System.Threading.Tasks.Task ExecuteCore()
55         {
56             if (string.IsNullOrEmpty(OutputDirectory))
57             {
58                 throw new ArgumentException("OutputDirectory is not set.");
59             }
60
61             DummyProject project = new DummyProject();
62
63             foreach (var source in Sources)
64             {
65                 string sourceFile = source.ItemSpec;
66                 if (!File.Exists(sourceFile))
67                 {
68                     throw new FileNotFoundException(sourceFile);
69                 }
70                 
71                 string generatedFile = Path.Combine(OutputDirectory, sourceFile);
72
73                 string targetDirectory = Path.GetDirectoryName(generatedFile);
74                 Directory.CreateDirectory(targetDirectory);
75
76                 await project.GenerateDummy(source.ItemSpec, generatedFile);
77                 _generatedFiles.Add(new TaskItem(generatedFile));
78             }
79         }
80     }
81 }