1# <pep8-80 compliant>
2
3# ##### BEGIN GPL LICENSE BLOCK #####
4#
5#  This program is free software; you can redistribute it and/or
6#  modify it under the terms of the GNU General Public License
7#  as published by the Free Software Foundation; either version 2
8#  of the License, or (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU General Public License for more details.
14#
15#  You should have received a copy of the GNU General Public License
16#  along with this program; if not, write to the Free Software Foundation,
17#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19# ##### END GPL LICENSE BLOCK #####
20
21__author__ = "Nutti <nutti.metro@gmail.com>"
22__status__ = "production"
23__version__ = "6.3"
24__date__ = "10 Aug 2020"
25
26import os
27
28import bpy
29from bpy.props import (
30    StringProperty,
31)
32
33from .utils.bl_class_registry import BlClassRegistry
34from .utils.addon_updater import (
35    AddonUpdaterManager,
36    AddonUpdaterConfig,
37    get_separator,
38)
39from .utils import compatibility as compat
40
41
42@BlClassRegistry()
43class MUV_OT_CheckAddonUpdate(bpy.types.Operator):
44    bl_idname = "uv.muv_check_addon_update"
45    bl_label = "Check Update"
46    bl_description = "Check Add-on Update"
47    bl_options = {'REGISTER', 'UNDO'}
48
49    def execute(self, _):
50        updater = AddonUpdaterManager.get_instance()
51        updater.check_update_candidate()
52
53        return {'FINISHED'}
54
55
56@BlClassRegistry()
57@compat.make_annotations
58class MUV_OT_UpdateAddon(bpy.types.Operator):
59    bl_idname = "uv.muv_update_addon"
60    bl_label = "Update"
61    bl_description = "Update Add-on"
62    bl_options = {'REGISTER', 'UNDO'}
63
64    branch_name = StringProperty(
65        name="Branch Name",
66        description="Branch name to update",
67        default="",
68    )
69
70    def execute(self, _):
71        updater = AddonUpdaterManager.get_instance()
72        updater.update(self.branch_name)
73
74        return {'FINISHED'}
75
76
77def draw_updater_ui(prefs_obj):
78    layout = prefs_obj.layout
79    updater = AddonUpdaterManager.get_instance()
80
81    layout.separator()
82
83    if not updater.candidate_checked():
84        col = layout.column()
85        col.scale_y = 2
86        row = col.row()
87        row.operator(MUV_OT_CheckAddonUpdate.bl_idname,
88                     text="Check 'Magic UV' add-on update",
89                     icon='FILE_REFRESH')
90    else:
91        row = layout.row(align=True)
92        row.scale_y = 2
93        col = row.column()
94        col.operator(MUV_OT_CheckAddonUpdate.bl_idname,
95                     text="Check 'Magic UV' add-on update",
96                     icon='FILE_REFRESH')
97        col = row.column()
98        if updater.latest_version() != "":
99            col.enabled = True
100            ops = col.operator(
101                MUV_OT_UpdateAddon.bl_idname,
102                text="Update to the latest release version (version: {})"
103                .format(updater.latest_version()),
104                icon='TRIA_DOWN_BAR')
105            ops.branch_name = updater.latest_version()
106        else:
107            col.enabled = False
108            col.operator(MUV_OT_UpdateAddon.bl_idname,
109                         text="No updates are available.")
110
111        layout.separator()
112        layout.label(text="Manual Update:")
113        row = layout.row(align=True)
114        row.prop(prefs_obj, "updater_branch_to_update", text="Target")
115        ops = row.operator(
116            MUV_OT_UpdateAddon.bl_idname, text="Update",
117            icon='TRIA_DOWN_BAR')
118        ops.branch_name = prefs_obj.updater_branch_to_update
119
120        layout.separator()
121        if updater.has_error():
122            box = layout.box()
123            box.label(text=updater.error(), icon='CANCEL')
124        elif updater.has_info():
125            box = layout.box()
126            box.label(text=updater.info(), icon='ERROR')
127
128
129def register_updater(bl_info):
130    config = AddonUpdaterConfig()
131    config.owner = "nutti"
132    config.repository = "Magic-UV"
133    config.current_addon_path = os.path.dirname(os.path.realpath(__file__))
134    config.branches = ["master", "develop"]
135    config.addon_directory = \
136        config.current_addon_path[
137            :config.current_addon_path.rfind(get_separator())]
138    config.min_release_version = bl_info["version"]
139    config.default_target_addon_path = "magic_uv"
140    config.target_addon_path = {
141        "master": "src{}magic_uv".format(get_separator()),
142        "develop": "src{}magic_uv".format(get_separator()),
143    }
144    updater = AddonUpdaterManager.get_instance()
145    updater.init(bl_info, config)
146